Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: problems with memories

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7

    problems with memories

    Hi, i don't know what i'm doing wrong, I want calc the sum of many values on array, but when i try put in a for loop with "sum = sum + 4;" for example, this lock my computer and i need reboot system.

    this is my code now:

    const char *KernelSource = "\n" \
    "__kernel void calcpi( \n" \
    " __global float* input, \n" \
    " __global float* output, \n" \
    " const unsigned int count) \n" \
    "{ \n" \
    " int i = get_global_id(0); \n" \
    " int n=100; int k = i*n; \n" \
    " __private float sum = 0; \n" \
    " float z = 0; \n" \
    " output[i] = 0; \n" \
    " for (k=i*n;k<i+n;k++) { \n" \
    " z = (float)k*2+1; \n" \
    " sum = sum + 4.0 / z; \n" \
    " } \n" \
    " if(i < count) \n" \
    " output[i] = sum; \n" \
    "} \n" \
    "\n";

    what i'm doing wrong? anyone can help me?
    Thank you

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: problems with memories

    .
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    7

    Re: problems with memories

    dot is a good answer =)

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    271

    Re: problems with memories

    might need to paste your relevant host code too.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    7

    Re: problems with memories

    Here is all of my code:

    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <OpenCL/opencl.h>
    ////////////////////////////////////////////////////////////////////////////////
    // Use a static data size for simplicity
    //
    #define DATA_SIZE (110000000)
    ////////////////////////////////////////////////////////////////////////////////
    #define TIPO float
    #define MULTI (100000000000000000000000000000000000.0f)
    // Simple compute kernel that computes the calcpi of an input array. [1]
    //
    const char *KernelSource = "\n" \
    "__kernel void calcpi( \n" \
    " __global float* input, \n" \
    " __global float* output, \n" \
    " const unsigned int count) \n" \
    "{ \n" \
    " int i = get_global_id(0); \n" \
    " int n=100; int k = i*n; \n" \
    " __private float sum = 0; \n" \
    " float z = 0; \n" \
    " output[i] = 0; \n" \
    " for (k=i*n;k<i+n;k++) { \n" \
    " z = (float)k*2+1; \n" \
    " sum = sum + 4.0f * 100000000000000000000000000000000000.0f / z; \n" \
    " } \n" \
    " if(i < count) \n" \
    " output[i] = sum; \n" \
    "} \n" \
    "\n";

    ////////////////////////////////////////////////////////////////////////////////

    int main(int argc, char** argv)
    {
    int err; // error code returned from api calls
    //printf("%d",sizeof(TIPO));
    //scanf("%d",&err);
    TIPO data[2]; // original data set given to device
    TIPO *results = malloc(sizeof(TIPO)*DATA_SIZE); // results returned from device
    //unsigned int correct; // number of correct results returned
    //printf("TESTE");

    size_t global; // global domain size for our calculation
    size_t local; // local domain size for our calculation

    cl_device_id device_id; // device ID
    cl_context context; // context
    cl_command_queue queue; // command queue
    cl_program program; // program
    cl_kernel kernel; // kernel

    cl_mem input; // device memory used for the input array
    cl_mem output; // device memory used for the output array

    // Get data on which to operate
    //

    //int i = 0;
    //int n = 3;
    unsigned long int count = DATA_SIZE;
    //for(i = 0; i < count; i+=2) {
    //data[i] = n;
    //n += 2;
    //}
    //printf("TESTE");
    // Get an ID for the device [2]
    int gpu = 1;
    err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1,&device_id, NULL);
    if (err != CL_SUCCESS)
    printf("ERROR clGetDeviceIDs!\n"); // [3]

    // Create a context [4]
    //
    context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
    if (!context) {
    printf("ERROR clCreateContext\n");
    }

    // Create a command queue [5]
    //
    queue = clCreateCommandQueue(context, device_id, 0, &err);
    if (!queue) {
    printf("ERROR clCreateCommandQueue\n");
    }

    // Create the compute program from the source buffer [6]
    //
    program = clCreateProgramWithSource(context, 1,(const char **) & KernelSource, NULL, &err);
    if ( !program) {
    printf("ERROR clCreateProgramWithSource\n");
    }

    // Build the program executable [7]
    //
    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"); //[8]
    clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG,sizeof(buffer), buffer, &len);
    printf("%s\n", buffer);
    exit(1);
    }

    // Create the compute kernel in the program we wish to run [9]
    //
    kernel = clCreateKernel(program, "calcpi", &err);
    if (!kernel || err != CL_SUCCESS) {
    printf("ERROR clCreateKernel OR CL_SUCESS\n");
    }

    // Create the input and output arrays in device memory for our calculation
    // [10]
    input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(TIPO)*2,NULL, NULL);
    output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(TIPO) *count,NULL, NULL);
    if (!input || !output) {
    printf("ERROR !INPUT OR !OUTPUT\n");
    }

    // Write our data set into the input array in device memory [11]
    //
    err = clEnqueueWriteBuffer(queue, input, CL_TRUE, 0,sizeof(TIPO)*2, data, 0, NULL, NULL);
    if (err != CL_SUCCESS) {
    printf("ERROR clEnqueueWriteBuffer\n");
    }

    // Set the arguments to our compute kernel [12]
    //
    err = 0;
    err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
    err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
    err |= clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
    if (err != CL_SUCCESS) {
    printf("ERROR ARGUMENTS COMPUTE KERNEL - ERROR NUMBER: %d\n",err);
    exit(1);
    }

    // Get the maximum work-group size for executing the kernel on the device
    // [13]
    err = clGetKernelWorkGroupInfo(kernel, device_id, CL_KERNEL_WORK_GROUP_SIZE,sizeof(size_t), &local, NULL);
    if (err != CL_SUCCESS) {
    printf("ERROR MAXIMUM WORK-GROUP - ERROR NUMBER: %d\n",err);
    exit(1);
    }

    // Execute the kernel over the entire range of the data set [14]
    //
    global = count;
    //printf("TESTE");
    err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL,0, NULL, NULL);
    if (err) {
    printf("ERROR EXECUTE KERNEL - ERROR NUMBER: %d\n",err);
    printf("ERROS: CL_INVALID_PROGRAM_EXECUTABLE %d\n", CL_INVALID_PROGRAM_EXECUTABLE);
    printf("ERROS: CL_INVALID_COMMAND_QUEUE %d\n", CL_INVALID_COMMAND_QUEUE);
    printf("ERROS: CL_INVALID_KERNEL %d\n", CL_INVALID_KERNEL);
    printf("ERROS: CL_INVALID_CONTEXT %d\n", CL_INVALID_CONTEXT);
    printf("ERROS: CL_INVALID_KERNEL_ARGS %d\n", CL_INVALID_KERNEL_ARGS);
    printf("ERROS: CL_INVALID_WORK_DIMENSION %d\n", CL_INVALID_WORK_DIMENSION);
    printf("ERROS: CL_INVALID_WORK_GROUP_SIZE %d\n", CL_INVALID_WORK_GROUP_SIZE);
    printf("ERROS: CL_MEM_OBJECT_ALLOCATION_FAILURE %d\n", CL_MEM_OBJECT_ALLOCATION_FAILURE);
    printf("ERROS: CL_INVALID_WORK_ITEM_SIZE %d\n", CL_INVALID_WORK_ITEM_SIZE);
    printf("ERROS: CL_INVALID_GLOBAL_OFFSET %d\n", CL_INVALID_GLOBAL_OFFSET);
    printf("ERROS: CL_OUT_OF_RESOURCES %d\n", CL_OUT_OF_RESOURCES);
    printf("ERROS: CL_INVALID_EVENT_WAIT_LIST %d\n", CL_INVALID_EVENT_WAIT_LIST);
    printf("ERROS: CL_OUT_OF_HOST_MEMORY %d\n", CL_OUT_OF_HOST_MEMORY);
    exit(1);
    }

    // Wait for the command queue to get serviced before reading back results
    // [15]
    clFinish(queue);

    // Read the results from the device [16]
    //
    err = clEnqueueReadBuffer(queue, output, CL_TRUE, 0,sizeof(TIPO) *count, results, 0, NULL, NULL );
    if (err != CL_SUCCESS) {
    printf("ERROR READ RESULTS - ERROR NUMBER: %d\n",err);
    printf("ERROS: CL_INVALID_PROGRAM_EXECUTABLE %d\n", CL_INVALID_PROGRAM_EXECUTABLE);
    printf("ERROS: CL_INVALID_COMMAND_QUEUE %d\n", CL_INVALID_COMMAND_QUEUE);
    printf("ERROS: CL_INVALID_KERNEL %d\n", CL_INVALID_KERNEL);
    printf("ERROS: CL_INVALID_CONTEXT %d\n", CL_INVALID_CONTEXT);
    printf("ERROS: CL_INVALID_KERNEL_ARGS %d\n", CL_INVALID_KERNEL_ARGS);
    printf("ERROS: CL_INVALID_WORK_DIMENSION %d\n", CL_INVALID_WORK_DIMENSION);
    printf("ERROS: CL_INVALID_WORK_GROUP_SIZE %d\n", CL_INVALID_WORK_GROUP_SIZE);
    printf("ERROS: CL_MEM_OBJECT_ALLOCATION_FAILURE %d\n", CL_MEM_OBJECT_ALLOCATION_FAILURE);
    printf("ERROS: CL_INVALID_WORK_ITEM_SIZE %d\n", CL_INVALID_WORK_ITEM_SIZE);
    printf("ERROS: CL_INVALID_GLOBAL_OFFSET %d\n", CL_INVALID_GLOBAL_OFFSET);
    printf("ERROS: CL_OUT_OF_RESOURCES %d\n", CL_OUT_OF_RESOURCES);
    printf("ERROS: CL_INVALID_EVENT_WAIT_LIST %d\n", CL_INVALID_EVENT_WAIT_LIST);
    printf("ERROS: CL_OUT_OF_HOST_MEMORY %d\n", CL_OUT_OF_HOST_MEMORY);
    exit(1);

    }
    //printf("TESTE");
    TIPO pi = 0.0;
    int i;
    for (i=0;i<count-1;i++) {
    //printf("%f",results[i]);
    pi += (pow(-1.0,i)) * (TIPO) results[i];
    //pi = (TIPO) results[i];
    printf("LOCAL ID: %f\n",results[i]);
    //printf("%f",(pow(-1,i)));
    //pi += (pow(-1.0,i));
    }
    printf("VE: 3.14159265358979323846\n");
    printf("PI: %1.60lf",pi/MULTI);

    // Shut down and clean up
    //
    clReleaseMemObject(input);
    clReleaseMemObject(output);
    clReleaseProgram(program);
    clReleaseKernel(kernel);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);

    scanf("%d",&i);
    return 0;
    }

    if anyone can help me i would appreciate very much.
    Thank you

  6. #6

    Re: problems with memories

    Dunno if this helps, but you need to change
    unsigned long int count = DATA_SIZE;
    to
    unsigned int count = DATA_SIZE;
    to match
    err |= clSetKernelArg(kernel, 2, sizeof(unsigned int), &count);
    and
    " const unsigned int count) \n" \

  7. #7

    Re: problems with memories

    Also the for-loop rarely runs, do you really want "k=i*n; k<i+n; k++", for example, k=i*n where i*n=4300 and k<i+n where i+n=143 would never execute.

  8. #8
    Senior Member
    Join Date
    Aug 2011
    Posts
    271

    Re: problems with memories

    Quote Originally Posted by bwatt
    Also the for-loop rarely runs, do you really want "k=i*n; k<i+n; k++", for example, k=i*n where i*n=4300 and k<i+n where i+n=143 would never execute.
    Actually it runs just 101 times?

    100 times for i=0
    1 time for i=1
    0 times for i=2 through 110000000

    Assuming the host code isn't printing errors about allocation failures (it will need to allocate 440 million bytes, which seems a lot), then there doesn't seem any obvious reason it should fail. Although it doesn't seem like it will do anything useful ...

    Try commenting out the call to clEnqueueNDRangeKernel() to make sure the rest is executing properly without having to reboot every time.

  9. #9

    Re: problems with memories

    BTW, I can run your code (with minor mods to adapt it) on my system, that is, "this locks my computer and i need reboot system" doesn't happen on my implementation.

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    7

    Re: problems with memories

    Hi, thank you for yours time.
    I try to do what you suggested, but doesn't work yet.
    I change the type value of "long unsigned int" for "unsigned int", try change the minor value of DATA_SIZE and commented line clEnqueueNDRangeKernel(), works fine, but nothing happen..
    and bwatt: what is your gpu? mine is a "AMD Radeon HD 6750M 1024 MB"

Page 1 of 2 12 LastLast

Similar Threads

  1. regarding some problems
    By chiraga213 in forum OpenCL
    Replies: 2
    Last Post: 01-29-2013, 11:49 PM
  2. Replies: 1
    Last Post: 09-05-2011, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •