4 Replies Latest reply on Nov 8, 2010 8:49 AM by supersunshine

    why the code does not work

    supersunshine

      i am new to opencl and  run the example  (gt100,vs2005+xp) ,but it does not work normally.

      all the results  are "-858993460" ,and i need help ,thanks!

      #include "stdio.h"
      #include "stdlib.h"
      #include <cl.h>isgust;
      #define  SIZE 20
      // *********************************************************************
       const char *OpenCLSource[]={
           "__kernel void VectorAdd(___global int *c,__global int *a,__global int *b)",
           "{",
           "int n=get_global_id(0);",
           "c[n]=a[n]+b[n];",
           "}"
       };
      //const char *OpenCLSource="Vector.cl";
      int main(int argc, char **argv)
      {
        int HostVect1[SIZE],HostVect2[SIZE],HostOutputVector[SIZE];
       
        for(int i=0;i<SIZE;i++){
        HostVect1=(rand()%1000);
        HostVect2
      =(rand()%1000);
        }

       
        cl_context GPUcontext=clCreateContextFromType(0,CL_DEVICE_TYPE_GPU,NULL,NULL,NULL);
        size_t ParmDataBytes;
        clGetContextInfo(GPUcontext,CL_CONTEXT_DEVICES,0,NULL,&ParmDataBytes);
        cl_device_id *GPUDevices=(cl_device_id*)malloc(sizeof(ParmDataBytes));
        clGetContextInfo(GPUcontext,CL_CONTEXT_DEVICES,ParmDataBytes,GPUDevices,NULL);

        cl_command_queue GPUCommandQueue=clCreateCommandQueue(GPUcontext,GPUDevices[0],0,NULL);


        cl_mem GPUVector1=clCreateBuffer(GPUcontext,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(int)*SIZE,HostVect1,NULL);
        cl_mem GPUVector2=clCreateBuffer(GPUcontext,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(int)*SIZE,HostVect2,NULL);
        cl_mem GPUOutputVecotr=clCreateBuffer(GPUcontext,CL_MEM_WRITE_ONLY,sizeof(int)*SIZE,NULL,NULL);

        clEnqueueWriteBuffer(GPUCommandQueue,GPUVector1,CL_TRUE,0,sizeof(int)*SIZE,HostVect1,0,NULL,NULL);
        clEnqueueWriteBuffer(GPUCommandQueue,GPUVector2,CL_TRUE,0,sizeof(int)*SIZE,HostVect2,0,NULL,NULL);

         cl_program OpenCLProgram=clCreateProgramWithSource(GPUcontext,5,OpenCLSource,NULL,NULL);
         clBuildProgram(OpenCLProgram,0,NULL,NULL,NULL,NULL);
         cl_kernel VectorAdd1=clCreateKernel(OpenCLProgram,"VectorAdd",NULL);

        clSetKernelArg(VectorAdd1,0,sizeof(cl_mem),(void *)&GPUOutputVecotr);
        clSetKernelArg(VectorAdd1,1,sizeof(cl_mem),(void *)&GPUVector1);
        clSetKernelArg(VectorAdd1,2,sizeof(cl_mem),(void *)&GPUVector2);

        size_t WorkSize[1]={SIZE};
        clEnqueueNDRangeKernel(GPUCommandQueue,VectorAdd1,1,NULL,WorkSize,NULL,0,NULL,NULL);

      // int HostOutputVector[SIZE];
        clEnqueueReadBuffer(GPUCommandQueue,GPUOutputVecotr,CL_TRUE,0,SIZE*sizeof(int),HostOutputVector,0,NULL,NULL);
       clFinish(GPUCommandQueue);
       


        HostOutputVector[3]=300;
        for(int rows=0;rows<SIZE;rows++,printf("\n")){
                 printf("%d",HostOutputVector[rows]);
        }
      free(GPUDevices);
        clReleaseKernel(VectorAdd1);
        clReleaseProgram(OpenCLProgram);
        clReleaseCommandQueue(GPUCommandQueue);
        clReleaseContext(GPUcontext);
        clReleaseMemObject(GPUVector1);
        clReleaseMemObject(GPUVector2);
        clReleaseMemObject(GPUOutputVecotr);
        getchar();
      return 0;   
      }