| 429 | } |
| 430 | |
| 431 | bool createProgram(cl_context context, cl_device_id device, string filename, cl_program* program) |
| 432 | { |
| 433 | cl_int errorNumber = 0; |
| 434 | ifstream kernelFile(filename.c_str(), ios::in); |
| 435 | |
| 436 | if(!kernelFile.is_open()) |
| 437 | { |
| 438 | cerr << "Unable to open " << filename << ". " << __FILE__ << ":"<< __LINE__ << endl; |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Read the kernel file into an output stream. |
| 444 | * Convert this into a char array for passing to OpenCL. |
| 445 | */ |
| 446 | ostringstream outputStringStream; |
| 447 | outputStringStream << kernelFile.rdbuf(); |
| 448 | string srcStdStr = outputStringStream.str(); |
| 449 | const char* charSource = srcStdStr.c_str(); |
| 450 | |
| 451 | *program = clCreateProgramWithSource(context, 1, &charSource, NULL, &errorNumber); |
| 452 | if (!checkSuccess(errorNumber) || program == NULL) |
| 453 | { |
| 454 | cerr << "Failed to create OpenCL program. " << __FILE__ << ":"<< __LINE__ << endl; |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | /* Try to build the OpenCL program. */ |
| 459 | bool buildSuccess = checkSuccess(clBuildProgram(*program, 0, NULL, NULL, NULL, NULL)); |
| 460 | |
| 461 | /* Get the size of the build log. */ |
| 462 | size_t logSize = 0; |
| 463 | clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize); |
| 464 | |
| 465 | /* |
| 466 | * If the build succeeds with no log, an empty string is returned (logSize = 1), |
| 467 | * we only want to print the message if it has some content (logSize > 1). |
| 468 | */ |
| 469 | if (logSize > 1) |
| 470 | { |
| 471 | char* log = new char[logSize]; |
| 472 | clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG, logSize, log, NULL); |
| 473 | |
| 474 | string* stringChars = new string(log, logSize); |
| 475 | cerr << "Build log:\n " << *stringChars << endl; |
| 476 | |
| 477 | delete[] log; |
| 478 | delete stringChars; |
| 479 | } |
| 480 | |
| 481 | if (!buildSuccess) |
| 482 | { |
| 483 | clReleaseProgram(*program); |
| 484 | cerr << "Failed to build OpenCL program. " << __FILE__ << ":"<< __LINE__ << endl; |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | return true; |
no test coverage detected