source: proiecte/NBody/NBody 2.0/SetupProgramCL.cpp @ 35

Last change on this file since 35 was 35, checked in by (none), 14 years ago
File size: 3.8 KB
Line 
1#include "NBody.hpp"
2
3/////////////////////////////////// NBody::setupCLProgram() Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
4//
5// Build CL kernel program executable.
6//
7/////////////////////////////////// NBody::setupCLProgram() Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
8
9int NBody::setupCLProgram()
10{
11        cl_int   status;
12
13        /* create a CL program using the kernel source */
14        char *program_source = load_program_source(kernelFileName);
15        size_t sourceSize[] = { strlen(program_source) };
16       
17    program = clCreateProgramWithSource(
18        context,
19        1,
20        (const char**)&program_source,
21        sourceSize,
22        &status);
23 
24        if (status != CL_SUCCESS){
25                std::cout << "clCreateProgramWithSource failed." << std::endl;
26                return 1;
27        }
28
29        /* create a cl program executable for all the devices specified */
30    status = clBuildProgram(
31                 program,
32                 1,
33                 &devices[0],
34                 NULL,
35                 NULL,
36                 NULL);
37   
38        if(status != CL_SUCCESS)
39    {
40        if(status == CL_BUILD_PROGRAM_FAILURE)
41        {
42            cl_int logStatus;
43            char * buildLog = NULL;
44            size_t buildLogSize = 0;
45            logStatus = clGetProgramBuildInfo (program, 
46                                            devices[0], 
47                                            CL_PROGRAM_BUILD_LOG, 
48                                            buildLogSize, 
49                                            buildLog, 
50                                            &buildLogSize);
51                        if (logStatus != CL_SUCCESS){
52                                std::cout << "clGetProgramBuildInfo failed." << std::endl;
53                                return 1;
54                        }
55           
56            buildLog = (char*)malloc(buildLogSize);
57            if(buildLog == NULL)
58            {
59                std::cout << "Failed to allocate host memory. (buildLog)" << std::endl;
60                return 1;
61            }
62            memset(buildLog, 0, buildLogSize);
63
64            logStatus = clGetProgramBuildInfo (program, 
65                                            devices[0], 
66                                            CL_PROGRAM_BUILD_LOG, 
67                                            buildLogSize, 
68                                            buildLog, 
69                                            NULL);
70                        if (logStatus != CL_SUCCESS){
71                                std::cout << "clGetProgramBuildInfo failed." << std::endl;
72                                free(buildLog);
73                                return 1;
74                        }
75       
76
77            std::cout << " \n\t\t\tBUILD LOG\n";
78            std::cout << " ************************************************\n";
79            std::cout << buildLog << std::endl;
80            std::cout << " ************************************************\n";
81            free(buildLog);
82        }
83       
84                if(status != CL_SUCCESS)
85        {
86            std::cout << "clBuildProgram failed." << std::endl;
87            return 1;
88        }
89    }
90}
91
92/////////////////////////////////// NBody::load_program_source Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
93//
94// Loads the kernel code form a .cl file type and returns the source code as a char*
95//
96/////////////////////////////////// NBody::load_program_source Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
97
98char* NBody::load_program_source(const char *filename)
99{ 
100        FILE *fh; 
101        char *source; 
102        int size;
103       
104        fh = fopen(filename, "r");
105        if (fh == NULL){
106                std::cout << "Reading the source cod for kernel failed." << std::endl;
107                exit(1); 
108        }
109       
110        fseek (fh, 0, SEEK_END);
111    size = ftell (fh);
112        rewind(fh);
113
114        source = (char *) malloc(size+1);
115
116        int result = fread(source, 1, size, fh);
117        /*if (result != size){
118                std::cout << "Reading the source cod for kernel failed." << std::endl;
119                exit(1);
120        }*/
121        source[result] = '\0';
122       
123        fclose(fh);
124               
125        return source; 
126} 
Note: See TracBrowser for help on using the repository browser.