cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

supersunshine
Journeyman III

why the code does not work

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=a+b;",
     "}"
 };
//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;   
}

0 Likes
4 Replies
himanshu_gautam
Grandmaster

Try to isolate the problem causing API using proper error handling mechanism.Check the value returned by different APIs.

0 Likes

but i would like to know if the code is wrong or right first ,specially ,the code in initail context etc. thanks.

0 Likes

thanks,i reslove the problem by using proper error handing mechanism.

0 Likes
nou
Exemplar

chceck return values for errors.

0 Likes