@@ -23,20 +23,34 @@ typedef struct {
23
23
cl_program program ;
24
24
} Common ;
25
25
26
- void common_init_options (
26
+ char * common_read_file (const char * path ) {
27
+ char * buffer ;
28
+ FILE * f ;
29
+ long length ;
30
+
31
+ f = fopen (path , "r" );
32
+ assert (NULL != f );
33
+ fseek (f , 0 , SEEK_END );
34
+ length = ftell (f );
35
+ fseek (f , 0 , SEEK_SET );
36
+ buffer = malloc (length + 1 );
37
+ if (fread (buffer , 1 , length , f ) < (size_t )length ) {
38
+ return NULL ;
39
+ }
40
+ fclose (f );
41
+ buffer [length ] = '\0' ;
42
+ return buffer ;
43
+ }
44
+
45
+ void common_create_kernel (
27
46
Common * common ,
28
47
const char * source ,
29
48
const char * options
30
49
) {
31
- char * err ;
32
- size_t err_len ;
33
50
cl_int ret ;
34
- cl_platform_id platform ;
51
+ char * err ;
52
+ size_t err_len ;
35
53
36
- clGetPlatformIDs (1 , & platform , NULL );
37
- clGetDeviceIDs (platform , CL_DEVICE_TYPE_ALL , 1 , & (common -> device ), NULL );
38
- common -> context = clCreateContext (NULL , 1 , & (common -> device ), NULL , NULL , NULL );
39
- common -> command_queue = clCreateCommandQueue (common -> context , common -> device , 0 , NULL );
40
54
if (NULL != source ) {
41
55
common -> program = clCreateProgramWithSource (common -> context , 1 , & source , NULL , NULL );
42
56
ret = clBuildProgram (common -> program , 1 , & (common -> device ), options , NULL , NULL );
@@ -55,30 +69,36 @@ void common_init_options(
55
69
}
56
70
}
57
71
58
- void common_init (
72
+ void common_create_kernel_file (
59
73
Common * common ,
60
- const char * source
74
+ const char * source_path ,
75
+ const char * options
61
76
) {
62
- common_init_options (common , source , "" );
77
+ char * source ;
78
+ source = common_read_file (source_path );
79
+ common_create_kernel (common , source , options );
80
+ free (source );
63
81
}
64
82
65
- char * common_read_file (const char * path ) {
66
- char * buffer ;
67
- FILE * f ;
68
- long length ;
83
+ void common_init_options (
84
+ Common * common ,
85
+ const char * source ,
86
+ const char * options
87
+ ) {
88
+ cl_platform_id platform ;
69
89
70
- f = fopen ( path , "r" );
71
- assert ( NULL != f );
72
- fseek ( f , 0 , SEEK_END );
73
- length = ftell ( f );
74
- fseek ( f , 0 , SEEK_SET );
75
- buffer = malloc ( length + 1 );
76
- if ( fread ( buffer , 1 , length , f ) < ( size_t ) length ) {
77
- return NULL ;
78
- }
79
- fclose ( f );
80
- buffer [ length ] = '\0' ;
81
- return buffer ;
90
+ clGetPlatformIDs ( 1 , & platform , NULL );
91
+ clGetDeviceIDs ( platform , CL_DEVICE_TYPE_ALL , 1 , & ( common -> device ), NULL );
92
+ common -> context = clCreateContext ( NULL , 1 , & ( common -> device ), NULL , NULL , NULL );
93
+ common -> command_queue = clCreateCommandQueue ( common -> context , common -> device , 0 , NULL );
94
+ common_create_kernel ( common , source , options );
95
+ }
96
+
97
+ void common_init (
98
+ Common * common ,
99
+ const char * source
100
+ ) {
101
+ common_init_options ( common , source , "" ) ;
82
102
}
83
103
84
104
void common_init_file_options (
@@ -96,7 +116,7 @@ void common_init_file(
96
116
Common * common ,
97
117
const char * source_path
98
118
) {
99
- common_init_file_options (common , source_path , "" );
119
+ common_init_file_options (common , source_path , "" );
100
120
}
101
121
102
122
void common_deinit (
@@ -111,7 +131,7 @@ void common_deinit(
111
131
clReleaseContext (common -> context );
112
132
}
113
133
#ifdef CL_1_2
114
- clReleaseDevice (common -> device );
134
+ clReleaseDevice (common -> device );
115
135
#endif
116
136
}
117
137
0 commit comments