allow specification of maximal amount of shared memory
[ppcg.git] / cuda_common.c
blob3722768c7d6472617934b2be33d1ec70edb4cbe6
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <ctype.h>
12 #include <limits.h>
13 #include <string.h>
15 #include "cuda_common.h"
17 static char *skip_spaces(char *s)
19 while (isspace(*s))
20 ++s;
21 return s;
24 static int is_begin_scop(char *line)
26 line = skip_spaces(line);
27 if (*line != '#')
28 return 0;
29 line = skip_spaces(line + 1);
30 if (strncmp(line, "pragma", sizeof("pragma") - 1))
31 return 0;
32 line = skip_spaces(line + sizeof("pragma") - 1);
33 if (strncmp(line, "scop", sizeof("scop") - 1))
34 return 0;
35 return 1;
38 static int is_end_scop(char *line)
40 line = skip_spaces(line);
41 if (*line != '#')
42 return 0;
43 line = skip_spaces(line + 1);
44 if (strncmp(line, "pragma", sizeof("pragma") - 1))
45 return 0;
46 line = skip_spaces(line + sizeof("pragma") - 1);
47 if (strncmp(line, "endscop", sizeof("endscop") - 1))
48 return 0;
49 return 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)
59 char name[PATH_MAX];
60 const char *base;
61 const char *ext;
62 int len;
63 char line[1024];
65 base = strrchr(input, '/');
66 if (base)
67 base++;
68 else
69 base = 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))
91 break;
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)
100 char line[1024];
102 while (fgets(line, sizeof(line), info->input)) {
103 if (is_end_scop(line)) {
104 fprintf(info->host_c, "%s", line);
105 break;
108 while (fgets(line, sizeof(line), info->input)) {
109 fprintf(info->host_c, "%s", line);
112 fclose(info->input);
113 fclose(info->kernel_c);
114 fclose(info->kernel_h);
115 fclose(info->host_c);