2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 #include "cuda_common.h"
17 static char *skip_spaces(char *s
)
24 static int is_begin_scop(char *line
)
26 line
= skip_spaces(line
);
29 line
= skip_spaces(line
+ 1);
30 if (strncmp(line
, "pragma", sizeof("pragma") - 1))
32 line
= skip_spaces(line
+ sizeof("pragma") - 1);
33 if (strncmp(line
, "scop", sizeof("scop") - 1))
38 static int is_end_scop(char *line
)
40 line
= skip_spaces(line
);
43 line
= skip_spaces(line
+ 1);
44 if (strncmp(line
, "pragma", sizeof("pragma") - 1))
46 line
= skip_spaces(line
+ sizeof("pragma") - 1);
47 if (strncmp(line
, "endscop", sizeof("endscop") - 1))
52 /* Open the "input" file for reading and open the host .cu file
53 * and the kernel .hu and .cu files for writing.
54 * Add the necessary includes and copy all code from the input
55 * file up to the openscop pragma to the host .cu file.
57 void cuda_open_files(struct cuda_info
*info
, const char *input
)
65 base
= strrchr(input
, '/');
70 ext
= strrchr(base
, '.');
71 len
= ext
? ext
- base
: strlen(base
);
73 memcpy(name
, base
, len
);
74 strcpy(name
+ len
, "_host.cu");
75 info
->host_c
= fopen(name
, "w");
77 strcpy(name
+ len
, "_kernel.cu");
78 info
->kernel_c
= fopen(name
, "w");
80 strcpy(name
+ len
, "_kernel.hu");
81 info
->kernel_h
= fopen(name
, "w");
82 fprintf(info
->host_c
, "#include <assert.h>\n");
83 fprintf(info
->host_c
, "#include \"%s\"\n", name
);
84 fprintf(info
->kernel_c
, "#include \"%s\"\n", name
);
85 fprintf(info
->kernel_h
, "#include \"cuda.h\"\n\n");
87 info
->input
= fopen(input
, "r");
88 while (fgets(line
, sizeof(line
), info
->input
)) {
89 fprintf(info
->host_c
, "%s", line
);
90 if (is_begin_scop(line
))
95 /* Copy all code starting at the endscop pragma from the input
96 * file to the host .cu file and close all input and output files.
98 void cuda_close_files(struct cuda_info
*info
)
102 while (fgets(line
, sizeof(line
), info
->input
)) {
103 if (is_end_scop(line
)) {
104 fprintf(info
->host_c
, "%s", line
);
108 while (fgets(line
, sizeof(line
), info
->input
)) {
109 fprintf(info
->host_c
, "%s", line
);
113 fclose(info
->kernel_c
);
114 fclose(info
->kernel_h
);
115 fclose(info
->host_c
);