• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ clCreateProgramWithBinary函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中clCreateProgramWithBinary函数的典型用法代码示例。如果您正苦于以下问题:C++ clCreateProgramWithBinary函数的具体用法?C++ clCreateProgramWithBinary怎么用?C++ clCreateProgramWithBinary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了clCreateProgramWithBinary函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: main


//.........这里部分代码省略.........
  //  b[i] = (int)i;
  //  results[i] = 0;
  //}
  //
  
  
  // Create a command commands
  commands = clCreateCommandQueue(context, device_id, 0, &err);
  if (!commands)
  {
    printf("Error: Failed to create a command commands!\n");
    printf("Error: code %i\n",err);
    printf("Test failed\n");
    return -1;
  }

  int status;

  // Create Program Objects
  //
  
  // Load binary from disk
  unsigned char *kernelbinary;
  char *xclbin=argv[1];
  printf("loading %s\n", xclbin);
  int n_i = load_file_to_memory(xclbin, (char **) &kernelbinary);
  if (n_i < 0) {
    printf("failed to load kernel from xclbin: %s\n", xclbin);
    printf("Test failed\n");
    return -1;
  }
  size_t n = n_i;
  // Create the compute program from offline
  program = clCreateProgramWithBinary(context, 1, &device_id, &n,
                                      (const unsigned char **) &kernelbinary, &status, &err);
  if ((!program) || (err!=CL_SUCCESS)) {
    printf("Error: Failed to create compute program from binary %d!\n", err);
    printf("Test failed\n");
    printf("err : %d %s\n",err,err);
  }

  // Build the program executable
  //
  err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  if (err != CL_SUCCESS)
  {
    size_t len;
    char buffer[2048];

    printf("Error: Failed to build program executable!\n");
    clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
    printf("%s\n", buffer);
    printf("Test failed\n");
    return -1;
  }

  // Create the compute kernel in the program we wish to run
  //
  kernel = clCreateKernel(program, "relu_1", &err);
  if (!kernel || err != CL_SUCCESS)
  {
    printf("Error: Failed to create compute kernel!\n");
    printf("Test failed\n");
    return -1;
  }
开发者ID:venkatesh20,项目名称:fpga_layers,代码行数:66,代码来源:relu_1_host.c


示例2: buildProgramFromAmdBin

int
buildProgramFromAmdBin(unsigned int platform_id,unsigned int dev_id,char *binFile)
{
    int i = 0;
    cl_int err = CL_SUCCESS;

    cl_int nPlatforms = 0;
    cl_platform_id *platforms = NULL;
    cl_platform_id platform = (cl_platform_id)NULL;
    cl_context_properties cprops[3];
    cl_context context;
    size_t nDevices = 0;
    cl_device_id devices[MAXGPUS];
    cl_device_id device_id = 0;
    size_t binary_size = 0;
    char * binary = NULL;
    cl_program program = NULL;
    char pbuf[100];
    cl_command_queue cmdq;
    cl_mem iBuf,oBuf;
    cl_kernel kernel;
    cl_int *inBuf,*outBuf;
    inBuf=(cl_int*)malloc(MAX_THREADS*sizeof(cl_int));
    outBuf=(cl_int*)malloc(MAX_THREADS*sizeof(cl_int));
    size_t N=MAX_THREADS;
    cl_event evnt;
    char buildOptions[200];
    char opencl_log[1024*64];

    /* figure out the number of platforms on this system. */
    err = clGetPlatformIDs( 0, NULL, &nPlatforms );
    checkErr( "clGetPlatformIDs", err );
    printf( "Number of platforms found: %d\n", nPlatforms );
    if( nPlatforms == 0 )
    {
        fprintf( stderr, "Cannot continue without any platforms. Exiting.\n" );
        return( -1 );
    }
    platforms = (cl_platform_id *)malloc( sizeof(cl_platform_id) * nPlatforms );
    err = clGetPlatformIDs( nPlatforms, platforms, NULL );
    checkErr( "clGetPlatformIDs", err );

    /* Check for AMD platform. */

    err = clGetPlatformInfo( platforms[platform_id], CL_PLATFORM_VENDOR,
                             sizeof(pbuf), pbuf, NULL );
    checkErr( "clGetPlatformInfo", err );
    if( strcmp(pbuf, "Advanced Micro Devices, Inc.") == 0 )
    {
        printf( "Found AMD platform\n" );
        platform = platforms[platform_id];

    }

    if( platform == (cl_context_properties)0 )
    {
        fprintf( stderr, "Could not find an AMD platform. Exiting.\n" );
        exit(0);
    }

    clGetDeviceIDs(platform,
                   CL_DEVICE_TYPE_ALL,MAXGPUS, devices, &nDevices);

    cprops[0] = CL_CONTEXT_PLATFORM;
    cprops[1] = (cl_context_properties)platform;
    cprops[2] = (cl_context_properties)NULL;

    context =   clCreateContext(cprops, 1, &devices[dev_id], NULL, NULL,
                                &err);
    checkErr( "clCreateContext", err );

    printDeviceName(dev_id,devices[dev_id]);

    /* read in the binary kernel. */
    binary = readKernelBin( &binary_size, binFile );

    /* create an OpenCL program from the binary kernel. */
    program = clCreateProgramWithBinary( context, 1, &devices[dev_id], &binary_size,
                                         (const unsigned char**)&binary, NULL, &err );
    checkErr( "clCreateProgramWithBinary", err );

    sprintf(buildOptions,"%s %s",OCL_BINARY_OPTIONS ,OCL_OPTIMIZATIONS);

    /* build the kernel source for all available devices in the context. */
    err = clBuildProgram( program, 0, NULL,buildOptions , NULL, NULL );

    checkErr("clGetProgramBuildInfo",clGetProgramBuildInfo(program, devices[dev_id],
             CL_PROGRAM_BUILD_LOG, sizeof(opencl_log), (void *) opencl_log,
             NULL));

    /*Report build errors and warnings*/
    if (err != CL_SUCCESS)
    {   fprintf(stderr, "Compilation log: %s\n", opencl_log);
        exit(0);
    }
#ifdef REPORT_OPENCL_WARNINGS
    else if (strlen(opencl_log) > 1)
        fprintf(stderr, "Compilation log: %s\n", opencl_log);
#endif

//.........这里部分代码省略.........
开发者ID:rarchk,项目名称:GPUbin,代码行数:101,代码来源:clbinuse.c


示例3: clGetPlatformIDs


//.........这里部分代码省略.........
                                   0,
                                   0,
                                   &status);
    if(!sampleCommon->checkVal(
        status,
        CL_SUCCESS,
        "clCreateBuffer failed. (inputImageBuffer)"))
    {
        return SDK_FAILURE;
    }

    if(!sampleCommon->checkVal(status,
        CL_SUCCESS,
        "clCreateBuffer failed. (outputImageBuffer)"))
    {
        return SDK_FAILURE;
    }

    /* create a CL program using the kernel source */
    streamsdk::SDKFile kernelFile;
    std::string kernelPath = sampleCommon->getPath();

    if(isLoadBinaryEnabled())
    {
        kernelPath.append(loadBinary.c_str());
        if(!kernelFile.readBinaryFromFile(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
            return SDK_FAILURE;
        }

        const char * binary = kernelFile.source().c_str();
        size_t binarySize = kernelFile.source().size();
        program = clCreateProgramWithBinary(context,
                                            1,
                                            &devices[deviceId], 
                                            (const size_t *)&binarySize,
                                            (const unsigned char**)&binary,
                                            NULL,
                                            &status);
        if(!sampleCommon->checkVal(status,
                                   CL_SUCCESS,
                                   "clCreateProgramWithBinary failed."))
        {
            return SDK_FAILURE;
        }

    }
    else
    {
        kernelPath.append("SimpleImage_Kernels.cl");
        if(!kernelFile.open(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : "<< kernelPath << std::endl;
            return SDK_FAILURE;
        }
        const char *source = kernelFile.source().c_str();
        size_t sourceSize[] = {strlen(source)};
        program = clCreateProgramWithSource(context,
            1,
            &source,
            sourceSize,
            &status);
        if(!sampleCommon->checkVal(
            status,
            CL_SUCCESS,
开发者ID:Karma-Revolution,项目名称:ocl-emu,代码行数:67,代码来源:SimpleImageEmu.cpp


示例4: calloc


//.........这里部分代码省略.........
	strcat(binaryfilename, numbuf);
	strcat(binaryfilename, ".bin");

loadbin:
	binaryfile = fopen(binaryfilename, "rb");
	if (!binaryfile) {
		applog(LOG_DEBUG, "No binary found, generating from source");
	} else {
		struct stat binary_stat;

		if (unlikely(stat(binaryfilename, &binary_stat))) {
			applog(LOG_DEBUG, "Unable to stat binary, generating from source");
			fclose(binaryfile);
			goto build;
		}
		if (!binary_stat.st_size)
			goto build;

		binary_sizes[slot] = binary_stat.st_size;
		binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
		if (unlikely(!binaries[slot])) {
			applog(LOG_ERR, "Unable to calloc binaries");
			fclose(binaryfile);
			return NULL;
		}

		if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
			applog(LOG_ERR, "Unable to fread binaries");
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
		if (status != CL_SUCCESS) {
			applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		clRetainProgram(clState->program);
		if (status != CL_SUCCESS) {
			applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
			return NULL;
		}

		fclose(binaryfile);
		applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);

		goto built;
	}

	/////////////////////////////////////////////////////////////////
	// Load CL file, build CL program object, create CL kernel object
	/////////////////////////////////////////////////////////////////

build:
	/* If no binary is available, and we have a card that suffers with phatk
	 * on SDK2.6, use the poclbm kernel instead if one has not been
	 * selected. */
	if (clState->chosen_kernel != KL_POCLBM && chosen_kernel == KL_NONE &&
		!strstr(name, "Tahiti") && clState->hasBitAlign &&
		(strstr(vbuff, "844.4") /* Linux 64 bit ATI 2.6 SDK */	||
		 strstr(vbuff, "851.4") /* Windows 64 bit "" */		||
		 strstr(vbuff, "831.4") /* Windows & Linux 32 bit "" */ )) {
开发者ID:holymonson,项目名称:cgminer,代码行数:67,代码来源:ocl.c


示例5: run_delaunay

void run_delaunay(cl_device_id device, bool is_cpu)
{
  curr_device = device;

  /* 
    Open Input Points 
  */
  scll points = open_points("inputpoints.bin");
  int num_points = points->count;  
   
  /* 
    Create Context
  */
  int error_code;
  context = clCreateContext(NULL, 1, &device, NULL, NULL, &error_code);
  if(error_code != 0)
    {
      printf("clCreateContext error code = %d\n", error_code);
      goto ExitFunction;
    }

  /*
    Create Command Queue
  */
  cl_command_queue_properties properties = 0;
  command_queue = clCreateCommandQueue(context, device, properties, &error_code);
  if(error_code != 0)
    {
      printf("clCreateCommandQueue error code ret=%d\n", error_code);
      goto ReleaseContext;
    }

  char * program_name;
  if(is_cpu)
    program_name = "cpu_kernel";
  else
    program_name = "cell_kernel";

  /*
    Open Program
  */
  size_t binary_length;
  unsigned char * binary;  
  OpenProgramBinary(program_name, &binary_length, &binary);
  cl_program program;
  cl_int binary_status;
  program = clCreateProgramWithBinary(context, 1, &device, &binary_length, (const unsigned char **) &binary, &binary_status, &error_code);
  if(error_code != 0)
    {
      printf("clCreateProgramWithBinary error code = %d\n", error_code);
      goto ReleaseCommandQueue;
    }

  char * kernel_name;
  if(is_cpu)
    kernel_name = "InCircle";
  else
    kernel_name = "cell_function";

  /*
    Open Kernel
  */
  kernel = clCreateKernel(program, kernel_name, &error_code);
  if(error_code != 0)
    {
      printf("clCreateKernel error code = %d\n", error_code);
      goto ReleaseProgram;
    }

  /* 
    Create buffers
  */
  points_mem = clCreateBuffer(context, CL_MEM_READ_WRITE, points->alloc_size, NULL, &error_code);
  if(error_code != 0)
    {
      printf("clCreateBuffer (points_mem) error code = %d\n", error_code);
      goto ReleaseKernel;
    }

  scll triangles = delaunay_core(points, num_points, is_cpu);
  //save_triangles("triangles.txt", triangles);

  clReleaseMemObject(points_mem);
ReleaseKernel:
  clReleaseKernel(kernel);
ReleaseProgram:
  clReleaseProgram(program);
ReleaseCommandQueue:
  clReleaseCommandQueue(command_queue);
ReleaseContext:
  clReleaseContext(context);
ExitFunction:
  return;
}
开发者ID:pcpratts,项目名称:gcc_opencl,代码行数:94,代码来源:delaunay.c


示例6: piglit_cl_build_program_with_binary_extended

cl_program
piglit_cl_build_program_with_binary_extended(piglit_cl_context context,
                                             size_t* lenghts,
                                             unsigned char** binaries,
                                             const char* options, bool fail)
{
	cl_int errNo;
	cl_program program;

	cl_int* binary_status = malloc(sizeof(cl_int) * context->num_devices);

	program = clCreateProgramWithBinary(context->cl_ctx,
	                                    context->num_devices,
	                                    context->device_ids,
	                                    lenghts,
	                                    (const unsigned char**)binaries,
	                                    binary_status,
	                                    &errNo);
	if(errNo != CL_SUCCESS) {
		int i;

		fprintf(stderr,
		        "Could not create program with binary: %s\n",
		        piglit_cl_get_error_name(errNo));

		printf("Create error with binaries:\n");
		for(i = 0; i < context->num_devices; i++) {
			char* device_name = piglit_cl_get_device_info(context->device_ids[i],
			                                              CL_DEVICE_NAME);
			
			printf("Error for %s: %s\n",
			       device_name,
			       piglit_cl_get_error_name(binary_status[i]));
			
			free(device_name);
		}

		free(binary_status);
		return NULL;
	}
	free(binary_status);
	
	errNo = clBuildProgram(program,
	                       context->num_devices,
	                       context->device_ids,
	                       options,
	                       NULL,
	                       NULL);
	if(   (!fail && errNo != CL_SUCCESS)
	   || ( fail && errNo == CL_SUCCESS)) {
		int i;

		fprintf(stderr,
		        !fail ? "Could not build program: %s\n"
		              : "Program built when it should have failed: %s\n",
		        piglit_cl_get_error_name(errNo));

		printf("Build log for binaries.\n");

		for(i = 0; i < context->num_devices; i++) {
			char* device_name = piglit_cl_get_device_info(context->device_ids[i],
			                                              CL_DEVICE_NAME);
			char* log = piglit_cl_get_program_build_info(program,
			                                             context->device_ids[i],
			                                             CL_PROGRAM_BUILD_LOG);
			
			printf("Build log for device %s:\n -------- \n%s\n -------- \n",
			       device_name,
			       log);
			
			free(device_name);
			free(log);
		}

		clReleaseProgram(program);
		return NULL;
	}

	return program;
}
开发者ID:aphogat,项目名称:piglit,代码行数:80,代码来源:piglit-util-cl.c


示例7: starpu_opencl_load_binary_opencl

int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs)
{
	unsigned int dev;
	unsigned int nb_devices;

	nb_devices = _starpu_opencl_get_device_count();
	// Iterate over each device
	for(dev = 0; dev < nb_devices; dev ++)
	{
		cl_device_id device;
		cl_context   context;
		cl_program   program;
		cl_int       err;
		char        *binary;
		char         binary_file_name[1024];
		size_t       length;
		cl_int       binary_status;

		opencl_programs->programs[dev] = NULL;

		starpu_opencl_get_device(dev, &device);
		starpu_opencl_get_context(dev, &context);
		if (context == NULL)
		{
			_STARPU_DEBUG("[%u] is not a valid OpenCL context\n", dev);
			continue;
		}

		// Load the binary buffer
		err = _starpu_opencl_get_binary_name(binary_file_name, 1024, kernel_id, dev, device);
		if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
		binary = _starpu_opencl_load_program_binary(binary_file_name, &length);

		// Create the compute program from the binary buffer
		program = clCreateProgramWithBinary(context, 1, &device, &length, (const unsigned char **) &binary, &binary_status, &err);
		if (!program || err != CL_SUCCESS)
		{
			_STARPU_DISP("Error: Failed to load program binary!\n");
			return EXIT_FAILURE;
		}

		// Build the program executable
		err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);

		// Get the status
		{
			cl_build_status status;
			size_t len;
			static char buffer[4096] = "";

			clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
			if (len > 2)
				_STARPU_DISP("Compilation output\n%s\n", buffer);

			clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_STATUS, sizeof(status), &status, NULL);
			if (err != CL_SUCCESS || status != CL_BUILD_SUCCESS)
			{
				_STARPU_DISP("Error: Failed to build program executable!\n");
				_STARPU_DISP("clBuildProgram: %d - clGetProgramBuildInfo: %d\n", err, status);
				return EXIT_FAILURE;
			}
		}

		// Store program
		opencl_programs->programs[dev] = program;
	}
	return 0;
}
开发者ID:joao-lima,项目名称:starpu-1.2.0rc2,代码行数:68,代码来源:driver_opencl_utils.c


示例8: clGetPlatformIDs


//.........这里部分代码省略.........
        return SDK_FAILURE;
    }

    /* Create memory objects for velocity */
    newVel = clCreateBuffer(
        context,
        CL_MEM_READ_ONLY,
        numBodies * sizeof(cl_float4),
        0,
        &status);
    if(!sampleCommon->checkVal(
        status,
        CL_SUCCESS,
        "clCreateBuffer failed. (newVel)"))
    {
        return SDK_FAILURE;
    }

    /* create a CL program using the kernel source */
    streamsdk::SDKFile kernelFile;
    std::string kernelPath = sampleCommon->getPath();

    if(isLoadBinaryEnabled())
    {
        kernelPath.append(loadBinary.c_str());
        if(!kernelFile.readBinaryFromFile(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
            return SDK_FAILURE;
        }

        const char * binary = kernelFile.source().c_str();
        size_t binarySize = kernelFile.source().size();
        program = clCreateProgramWithBinary(context,
                                            1,
                                            &devices[deviceId], 
                                            (const size_t *)&binarySize,
                                            (const unsigned char**)&binary,
                                            NULL,
                                            &status);
        if(!sampleCommon->checkVal(status,
                                   CL_SUCCESS,
                                   "clCreateProgramWithBinary failed."))
        {
            return SDK_FAILURE;
        }

    }
    else
    {
        kernelPath.append("NBody_Kernels.cl");
        if(!kernelFile.open(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
            return SDK_FAILURE;
        }
        const char * source = kernelFile.source().c_str();
        size_t sourceSize[] = { strlen(source) };
        program = clCreateProgramWithSource(context,
                                            1,
                                            &source,
                                            sourceSize,
                                            &status);
        if(!sampleCommon->checkVal(
            status,
            CL_SUCCESS,
开发者ID:pbains,项目名称:m2s-bench-amdapp-2.5,代码行数:67,代码来源:NBody.cpp


示例9: clGetPlatformIDs


//.........这里部分代码省略.........
        return SDK_FAILURE;
    }

    /* Create memory object for temp Image */
    tempImageBuffer = clCreateBuffer(
                          context,
                          CL_MEM_READ_WRITE,
                          width * height * pixelSize,
                          0,
                          &status);
    if(!sampleCommon->checkVal(
                status,
                CL_SUCCESS,
                "clCreateBuffer failed. (tempImageBuffer)"))
    {
        return SDK_FAILURE;
    }

    /* create a CL program using the kernel source */
    streamsdk::SDKFile kernelFile;
    std::string kernelPath = sampleCommon->getPath();

    if(isLoadBinaryEnabled())
    {
        kernelPath.append(loadBinary.c_str());
        if(!kernelFile.readBinaryFromFile(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
            return SDK_FAILURE;
        }

        const char * binary = kernelFile.source().c_str();
        size_t binarySize = kernelFile.source().size();
        program = clCreateProgramWithBinary(context,
                                            1,
                                            &devices[deviceId],
                                            (const size_t *)&binarySize,
                                            (const unsigned char**)&binary,
                                            NULL,
                                            &status);
        if(!sampleCommon->checkVal(status,
                                   CL_SUCCESS,
                                   "clCreateProgramWithBinary failed."))
        {
            return SDK_FAILURE;
        }

    }
    else
    {
        kernelPath.append("BoxFilterGL_Kernels.cl");
        if(!kernelFile.open(kernelPath.c_str()))
        {
            std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
            return SDK_FAILURE;
        }
        const char *source = kernelFile.source().c_str();
        size_t sourceSize[] = {strlen(source)};
        program = clCreateProgramWithSource(context,
                                            1,
                                            &source,
                                            sourceSize,
                                            &status);
        if(!sampleCommon->checkVal(
                    status,
                    CL_SUCCESS,
开发者ID:pbains,项目名称:m2s-bench-amdapp-2.5,代码行数:67,代码来源:BoxFilterGLSeparable.cpp


示例10: initFPGA

struct cl_package initFPGA( const char* xclbin, const char* kernel_name )
{
	/*****************************************/
	/* Initialize OpenCL */
	/*****************************************/

	// Retrieve the number of platforms
    cl_uint numPlatforms = 0;
    cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms);

	//printf("Found %d platforms support OpenCL, return code %d.\n", numPlatforms, status);
 
    // Allocate enough space for each platform
    cl_platform_id *platforms = (cl_platform_id*)malloc( numPlatforms*sizeof(cl_platform_id));
 
    status = clGetPlatformIDs(numPlatforms, platforms, NULL);
	if (status != CL_SUCCESS)
		printf("clGetPlatformIDs error(%d)\n", status);
	
	// Retrieve the number of devices
    cl_uint numDevices = 0;
#ifndef FPGA_DEVICE
    status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
#else
    status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ACCELERATOR, 0, NULL, &numDevices);
#endif
	printf("Found %d devices support OpenCL.\n", numDevices);

    // Allocate enough space for each device
    cl_device_id *devices = (cl_device_id*)malloc( numDevices*sizeof(cl_device_id));

    // Fill in the devices 
#ifndef FPGA_DEVICE
    status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, numDevices, devices, NULL);
#else
    status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ACCELERATOR, numDevices, devices, NULL);
#endif
	
	if (status != CL_SUCCESS)
		printf("clGetDeviceIDs error(%d)\n", status);

    // Create a context and associate it with the devices
    cl_context context;
    context = clCreateContext(NULL, numDevices, devices, NULL, NULL, &status);
	if (status != CL_SUCCESS)
		printf("clCreateContext error(%d)\n", status);


	//Create a command-queue
	cl_command_queue clCommandQue = clCreateCommandQueue(context, devices[0], 0, &status);

	if (status != CL_SUCCESS)
		printf("clCreateCommandQueue error(%d)\n", status);

	// 6. Load and build OpenCL kernel
	
#ifndef FPGA_DEVICE
	// Create a program with source code
    cl_program program = clCreateProgramWithSource(context, 1, 
        (const char**)&logistic_cl, NULL, &status);
	if (status != 0)
		printf("clCreateProgramWithSource error(%d)\n", status);

    // Build (compile) the program for the device
    status = clBuildProgram(program, 1, devices, NULL, NULL, NULL);
#else
	// Load binary from disk
	unsigned char *kernelbinary;
	printf("loading %s\n", xclbin);
	int n_i = load_file_to_memory(xclbin, (char **) &kernelbinary);
	if (n_i < 0) {
		printf("ERROR: failed to load kernel from xclbin: %s\n", xclbin);
		exit(1);
	}
	size_t n_bit = n_i;

	// Create the compute program from offline
	cl_program program = clCreateProgramWithBinary(context, 1, &devices[0], &n_bit,
			(const unsigned char **) &kernelbinary, NULL, &status);
	if ((!program) || (status != CL_SUCCESS)) {
		printf("Error: Failed to create compute program from binary %d!\n", status);
		exit(1);
	}

	// Build the program executable
	status = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
#endif

	if (status != 0) {
		char errmsg[2048];
		size_t sizemsg = 0;

		status = clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 2048*sizeof(char), errmsg, &sizemsg);

		printf("clBuildProgram error(%d)\n", status);
		printf("Compilation messages: \n %s", errmsg);
	}

	cl_kernel clKernel = clCreateKernel(program, kernel_name, &status);
	if (status != CL_SUCCESS)
//.........这里部分代码省略.........
开发者ID:alondight09,项目名称:spark,代码行数:101,代码来源:accService.c


示例11: pclu_create_program

pclu_program* 
pclu_create_program(pclu_context* pclu, const char* path)
{
    int errcode;
    
    pclu_program* pgm = (pclu_program*) malloc(sizeof(pclu_program));
    pgm->pclu      = pclu;
    pgm->build_log = 0;

#define LOAD_BINS 1

#if LOAD_BINS

    const char* binary = (const char*) pclu_slurp_file("fmma.ptx");
    size_t size = strlen(binary);

    const unsigned char** bins = (const unsigned char**) binary;
    int status;

    pgm->program = clCreateProgramWithBinary(pclu->context, 1, &(pclu->device), 
            &size, bins, &status, &errcode);

    pclu_check_call("clCreateProgramWithBinary", errcode);
    pclu_check_call("clCreateProgramWithBinary status", status);

#else

    /* Read the source from disk */
    char* source = pclu_slurp_file(path);
    size_t  size = strlen(source);

    const char** sources = (const char**) &source;

    pgm->program = clCreateProgramWithSource(pclu->context, 1, sources, &size, &errcode);
    pclu_check_call("clCreateProgramWithSource", errcode);

    free(source);

    /* Compile for the device */
    errcode = clBuildProgram(pgm->program, 1, &(pclu->device), "", 0, 0);

    /* Print out errors on failure */
    if (errcode == CL_BUILD_PROGRAM_FAILURE) {
        size_t log_size;
        char*  log_text;

        pclu_check_call("clGetProgramBuildInfo", 
                clGetProgramBuildInfo(
                    pgm->program, pclu->device, CL_PROGRAM_BUILD_LOG, 0, 0, &log_size));

        log_text = (char*) alloca(log_size);

        pclu_check_call("clGetProgramBuildInfo", 
                clGetProgramBuildInfo(
                    pgm->program, pclu->device, CL_PROGRAM_BUILD_LOG, 
                    log_size, log_text, 0));

        fprintf(stderr, "Build Errors\n%s\n", log_text);
    }

    pclu_check_call("clBuildProgram", errcode);
#endif

#if DUMP_BINS
    /* Dump the Binaries */
    size_t bin_size;
    errcode = clGetProgramInfo(pgm->program, CL_PROGRAM_BINARY_SIZES, 
            sizeof(size_t), &bin_size, 0);
    pclu_check_call("clGetProgramInfo(BIN_SIZE)", errcode);

    cl_uchar* binary = (cl_uchar*) malloc(bin_size);
    errcode = clGetProgramInfo(pgm->program, CL_PROGRAM_BINARIES, bin_size, &binary, 0);
    pclu_check_call("clGetProgramInfo(BINARIES)", errcode);

    FILE* bf = fopen("opencl.bin", "w");
    fwrite((void*)binary, bin_size, 1, bf);
    fclose(bf);

    free(binary);
#endif

    /* Get the kernels */

    /*
    pclu_check_call("clCreateKernelsInProgram",
		    clCreateKernelsInProgram(pgm->program, 0, 0, &(pgm->num_kernels)));

    pgm->kernels = (cl_kernel*) malloc(pgm->num_kernels*sizeof(cl_kernel));
    pclu_check_call("clCreateKernelsInProgram",
		    clCreateKernelsInProgram(pgm->program, pgm->num_kernels, pgm->kernels, 0));
    */

    return pgm;
}
开发者ID:NatTuck,项目名称:cakemark,代码行数:94,代码来源:pclu.c


示例12: calloc


//.........这里部分代码省略.........
	strcat(binaryfilename, "long");
	sprintf(numbuf, "%d", (int)sizeof(long));
	strcat(binaryfilename, numbuf);
	strcat(binaryfilename, ".bin");

	binaryfile = fopen(binaryfilename, "rb");
	if (!binaryfile) {
		if (opt_debug)
			applog(LOG_DEBUG, "No binary found, generating from source");
	} else {
		struct stat binary_stat;

		if (unlikely(stat(binaryfilename, &binary_stat))) {
			if (opt_debug)
				applog(LOG_DEBUG, "Unable to stat binary, generating from source");
			fclose(binaryfile);
			goto build;
		}
		binary_sizes[gpu] = binary_stat.st_size;
		binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
		if (unlikely(!binaries[gpu])) {
			applog(LOG_ERR, "Unable to malloc binaries");
			fclose(binaryfile);
			return NULL;
		}

		if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
			applog(LOG_ERR, "Unable to fread binaries[gpu]");
			fclose(binaryfile);
			goto build;
		}
		fclose(binaryfile);

		clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
		if (status != CL_SUCCESS)
		{
			applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
			return NULL;
		}
		if (opt_debug)
			applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);

		free(binaries[gpu]);
		goto built;
	}

	/////////////////////////////////////////////////////////////////
	// Load CL file, build CL program object, create CL kernel object
	/////////////////////////////////////////////////////////////////

build:
	memcpy(source, rawsource, pl);

	/* Patch the source file with the preferred_vwidth */
	if (clState->preferred_vwidth > 1) {
		char *find = strstr(source, "VECTORSX");

		if (unlikely(!find)) {
			applog(LOG_ERR, "Unable to find VECTORSX in source");
			return NULL;
		}
		find += 7; // "VECTORS"
		if (clState->preferred_vwidth == 2)
			strncpy(find, "2", 1);
		else
			strncpy(find, "4", 1);
开发者ID:furyan,项目名称:cgminer,代码行数:67,代码来源:ocl.c


示例13: main

int
main(int argc, char** argv)
{


   srand(1000);
   int i;

   unsigned int size_A = WA * HA;
   unsigned int mem_size_A = sizeof(float) * size_A;
   float* h_A = (float*) malloc(mem_size_A);

   unsigned int size_B = WB * HB;
   unsigned int mem_size_B = sizeof(float) * size_B;
   float* h_B = (float*) malloc(mem_size_B);


   randomInit(h_A, size_A);
   randomInit(h_B, size_B);


   unsigned int size_C = WC * HC;
   unsigned int mem_size_C = sizeof(float) * size_C;
   float* h_C = (float*) malloc(mem_size_C);

   cl_context clGPUContext;
   cl_command_queue clCommandQue;
   cl_program clProgram;
   cl_kernel clKernel;
   cl_event mm;

   size_t dataBytes;
   size_t kernelLength;
   cl_int errcode;


   cl_mem d_A;
   cl_mem d_B;
   cl_mem d_C;


   clGPUContext = clCreateContextFromType(0,
                   CL_DEVICE_TYPE_GPU,
                   NULL, NULL, &errcode);



   errcode = clGetContextInfo(clGPUContext,
              CL_CONTEXT_DEVICES, 0, NULL,
              &dataBytes);
   cl_device_id *clDevices = (cl_device_id *)
              malloc(dataBytes);
   errcode |= clGetContextInfo(clGPUContext,
              CL_CONTEXT_DEVICES, dataBytes,
              clDevices, NULL);



   clCommandQue = clCreateCommandQueue(clGPUContext,
                  clDevices[0], CL_QUEUE_PROFILING_ENABLE, &errcode);



   d_C = clCreateBuffer(clGPUContext,
          CL_MEM_READ_WRITE,
          mem_size_A, NULL, &errcode);
   d_A = clCreateBuffer(clGPUContext,
          CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
          mem_size_A, h_A, &errcode);
   d_B = clCreateBuffer(clGPUContext,
          CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
          mem_size_B, h_B, &errcode);


   FILE* fp = fopen("hw2.cl", "r");
   fseek (fp , 0 , SEEK_END);
   const size_t lSize = ftell(fp);
   rewind(fp);
   unsigned char* buffer;
   buffer = (unsigned char*) malloc (lSize);
   fread(buffer, 1, lSize, fp);
   fclose(fp);

   cl_int status;
   clProgram = clCreateProgramWithBinary(clGPUContext,
                1, (const cl_device_id *)clDevices,
                &lSize, (const unsigned char**)&buffer,
                &status, &errcode);
   errcode = clBuildProgram(clProgram, 0, NULL, NULL,
                NULL, NULL);


   errcode = clBuildProgram(clProgram, 0,
              NULL, NULL, NULL, NULL);


   clKernel = clCreateKernel(clProgram,
               "MM", &errcode);


//.........这里部分代码省略.........
开发者ID:hemantjp,项目名称:HW2,代码行数:101,代码来源:hw2.c


示例14: halide_init_kernels


//.........这里部分代码省略.........
        const char * dev_type = getenv("HL_OCL_DEVICE");
        if (dev_type != NULL) {
            if (strstr("cpu", dev_type))
                device_type |= CL_DEVICE_TYPE_CPU;
            if (strstr("gpu", dev_type))
                device_type |= CL_DEVICE_TYPE_GPU;
        } 
        // If no devices are specified yet, just use all.
        if (device_type == 0)
            device_type = CL_DEVICE_TYPE_ALL;
        
        // Make sure we have a device
        const cl_uint maxDevices = 4;
        cl_device_id devices[maxDevices];
        cl_uint deviceCount = 0;
        err = clGetDeviceIDs( platform, device_type, maxDevices, devices, &deviceCount );
        CHECK_ERR( err, "clGetDeviceIDs" );
        if (deviceCount == 0) {
            halide_printf(user_context, "Failed to get device\n");
            return;
        }

        dev = devices[deviceCount-1];

        #ifdef DEBUG
        const cl_uint maxDeviceName = 256;
        char deviceName[maxDeviceName];
        err = clGetDeviceInfo( dev, CL_DEVICE_NAME, maxDeviceName, deviceName, NULL );
        CHECK_ERR( err, "clGetDeviceInfo" );

        halide_printf(user_context, "Got device '%s', about to create context (t=%lld)\n",
                      deviceName, (long long)halide_current_time_ns(user_context));
        #endif


        // Create context
        cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
        *cl_ctx = clCreateContext(properties, 1, &dev, NULL, NULL, &err);
        CHECK_ERR( err, "clCreateContext" );
        // cuEventCreate(&__start, 0);
        // cuEventCreate(&__end, 0);

        halide_assert(user_context, !(*cl_q));
        *cl_q = clCreateCommandQueue(*cl_ctx, dev, 0, &err);
        CHECK_ERR( err, "clCreateCommandQueue" );
    } else {
        #ifdef DEBUG
        halide_printf(user_context, "Already had context %p\n", *cl_ctx);
        #endif

        // Maintain ref count of context.
        CHECK_CALL( clRetainContext(*cl_ctx), "clRetainContext" );
        CHECK_CALL( clRetainCommandQueue(*cl_q), "clRetainCommandQueue" );

        CHECK_CALL( clGetContextInfo(*cl_ctx, CL_CONTEXT_DEVICES, sizeof(dev), &dev, NULL), "clGetContextInfo" );
    }

    // Initialize a module for just this Halide module
    if ((!__mod) && (size > 1)) {
        // Create module

        cl_device_id devices[] = { dev };
        size_t lengths[] = { size };

        if (strstr(src, "/*OpenCL C*/")) {
            // Program is OpenCL C.

            #ifdef DEBUG
            halide_printf(user_context, "Compiling OpenCL C kernel: %s\n\n", src);
            #endif

            const char * sources[] = { src };
            __mod = clCreateProgramWithSource(*cl_ctx, 1, &sources[0], NULL, &err );
            CHECK_ERR( err, "clCreateProgramWithSource" );
        } else {
            // Program is SPIR binary.

            #ifdef DEBUG
            halide_printf(user_context, "Compiling SPIR kernel (%i bytes)\n", size);
            #endif

            const unsigned char * binaries[] = { (unsigned char *)src };
            __mod = clCreateProgramWithBinary(*cl_ctx, 1, devices, lengths, &binaries[0], NULL, &err );
            CHECK_ERR( err, "clCreateProgramWithBinary" );
        }

        err = clBuildProgram( __mod, 1, &dev, NULL, NULL, NULL );
        if (err != CL_SUCCESS) {
            size_t len;
            char buffer[2048];

            halide_printf(user_context, "Error: Failed to build program executable! err = %d\n", err);
            if (clGetProgramBuildInfo(__mod, dev, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len) == CL_SUCCESS)
                halide_printf(user_context, "Build Log:\n %s\n-----\n", buffer);
            else
                halide_printf(user_context, "clGetProgramBuildInfo failed to get build log!\n");
            halide_assert(user_context, err == CL_SUCCESS);
        }
    }
}
开发者ID:EEmmanuel7,项目名称:Halide,代码行数:101,代码来源:opencl.cpp


示例15: main


//.........这里部分代码省略.........
    printf("Error: Failed to create a compute context!\n");
    printf("Test failed\n");
    return EXIT_FAILURE;
  }

  // Create a command commands
  //
  commands = clCreateCommandQueue(context, device_id, 0, &err);
  if (!commands)
  {
    printf("Error: Failed to create a command commands!\n");
    printf("Error: code %i\n",err);
    printf("Test failed\n");
    return EXIT_FAILURE;
  }

  int status;

  // Create Program Objects
  //
  
  // Load binary from disk
  unsigned char *kernelbinary;
  char *xclbin=argv[1];
  printf("loading %s\n", xclbin);
  int n_i = load_file_to_memory(xclbin, (char **) &kernelbinary);
  if (n_i < 0) {
    printf("failed to load kernel from xclbin: %s\n", xclbin);
    printf("Test failed\n");
    return EXIT_FAILURE;
  }
  size_t n = n_i;
  // Create the compute program from offline
  program = clCreateProgramWithBinary(context, 1, &device_id, &n,
                                      (const unsigned char **) &kernelbinary, &status, &err);
  if ((!program) || (err!=CL_SUCCESS)) {
    printf("Error: Failed to create compute program from binary %d!\n", err);
    printf("Test failed\n");
    return EXIT_FAILURE;
  }

  // Build the program executable
  //
  err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  if (err != CL_SUCCESS)
  {
    size_t len;
    char buffer[2048];

    printf("Error: Failed to build program executable!\n");
    clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
    printf("%s\n", buffer);
    printf("Test failed\n");
    return EXIT_FAILURE;
  }

  // Create the compute kernel in the program we wish to run
  //
  kernel = clCreateKernel(program, "conv3_layer", &err);
  if (!kernel || err != CL_SUCCESS)
  {
    printf("Error: Failed to create compute kernel!\n");
    printf("Test failed\n");
    return EXIT_FAILURE;
  }
开发者ID:venkatesh20,项目名称:fpga_layers,代码行数:66,代码来源:test-cl.c


示例16: sizeof


//.........这里部分代码省略.........
	sprintf(strbuf, "l%d", (int)sizeof(long));
	strcat(binaryfilename, strbuf);
	strcat(binaryfilename, ".bin");

	binaryfile = fopen(binaryfilename, "rb");
	if (!binaryfile) {
		applog(LOG_DEBUG, "No binary found, generating from source");
	} else {
		struct stat binary_stat;

		if (unlikely(stat(binaryfilename, &binary_stat))) {
			applog(LOG_DEBUG, "Unable to stat binary, generating from source");
			fclose(binaryfile);
			goto build;
		}
		if (!binary_stat.st_size)
			goto build;

		binary_sizes[slot] = binary_stat.st_size;
		binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
		if (unlikely(!binaries[slot])) {
			applog(LOG_ERR, "Unable to calloc binaries");
			fclose(binaryfile);
			return NULL;
		}

		if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
			applog(LOG_ERR, "Unable to fread binaries");
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
		if (status != CL_SUCCESS) {
			applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
			fclose(binaryfile);
			free(binaries[slot]);
			goto build;
		}

		fclose(binaryfile);
		applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);

		goto built;
	}

	/////////////////////////////////////////////////////////////////
	// Load CL file, build CL program object, create CL kernel object
	/////////////////// 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ clEnqueueNDRangeKernel函数代码示例发布时间:2022-05-30
下一篇:
C++ clCreateKernel函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap