Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Compose a programme using OpenCL and using local memory. Task: b=min(A+C)

I have a ready block diagram, but the teacher says that it shows everything sequentially, and we have a parallel programme and I don't know how to fix the block diagram.

I also want to know if I have written a programme using local memory correctly or not.

Link to image with block diagram: https://www.imghippo.com/i/1704956923.jpg

#define _CRT_SECURE_NO_WARNINGS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include <CL/cl.h>
#include <CL/cl_platform.h>
#include <iostream>
#include <cstdlib>
#include <stdio.h>

#define MAX_SOURCE_SIZE (0x100000)
#define LIST_SIZE 64

int main(void) {
    int* A = new int[LIST_SIZE];
    int* C = new int[LIST_SIZE];
    int* B = new int[LIST_SIZE];
    srand(time(0));
    for (int i = 0; i < LIST_SIZE; i++) {
        A[i] = (rand() % 200) / 2.;
        C[i] = (rand() % 200) / 2.;
    }

    FILE* fp;
    char* source_str;
    size_t source_size;

    fp = fopen("kernel.cl", "r");
    if (!fp) {
        fprintf(stderr, "Failed to load kernel.\n");
        exit(1);
    }
    source_str = new char[MAX_SOURCE_SIZE];
    source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
    fclose(fp);

    cl_platform_id platform_id = NULL;
    cl_device_id device_id = NULL;
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
    cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
    ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);

    cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);

    cl_command_queue command_queue = clCreateCommandQueueWithProperties(context, device_id, 0, &ret);

    cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
    cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
    cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);

    ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
    ret = clEnqueueWriteBuffer(command_queue, c_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), C, 0, NULL, NULL);

    cl_program program = clCreateProgramWithSource(context, 1, (const char**)&source_str, (const size_t*)&source_size, &ret);

    ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);

    cl_kernel kernel = clCreateKernel(program, "krn", &ret);

    size_t local_item_size = 64;
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&a_mem_obj);
    ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&c_mem_obj);
    ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&b_mem_obj);
    ret = clSetKernelArg(kernel, 3, local_item_size * sizeof(int), NULL);

    size_t global_item_size = LIST_SIZE;
    ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);

    ret = clEnqueueReadBuffer(command_queue, b_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), B, 0, NULL, NULL);

    for (int i = 0; i < LIST_SIZE; i++) {
        printf("%d. sum: %d\n", i, B[i]);
    }

    int min = B[0];
    int minIndex = 0;
    for (int i = 0; i < LIST_SIZE; i++) {
        if (B[i] < min) {
            min = B[i];
            minIndex = i;
        }
    }
    printf("\n\n%d. Minimum min(A+C) = %d\n", minIndex, min);

    ret = clFlush(command_queue);
    ret = clFinish(command_queue);
    ret = clReleaseKernel(kernel);
    ret = clReleaseProgram(program);
    ret = clReleaseMemObject(a_mem_obj);
    ret = clReleaseMemObject(c_mem_obj);
    ret = clReleaseMemObject(b_mem_obj);
    ret = clReleaseCommandQueue(command_queue);
    ret = clReleaseContext(context);
    delete[] source_str;
    delete[] A;
    delete[] B;
    delete[] C;
    return 0;
}


kernel.cl:
__kernel void krn(__global const int* A, __global const int* C, __global int* B) {
    int i = get_global_id(0); 
    __local int localA[128];
    __local int localC[128];
    localA[i] = A[i];
    localC[i] = C[i];
    barrier(CLK_LOCAL_MEM_FENCE);
    B[i] = localA[i] + localC[i];
}


What I have tried:

Made a wrong block diagram and wrote a programme
Posted
Updated 10-Jan-24 20:50pm
v9

1 solution

Um. No.

That's not a diagram of how your app should work: it's a mishmash of an app and instructions to produce it - and if your assignment wants you to produce a parallel based app, then you need to examine the assignment carefully and identify what parts can be done simultaneously and independently, which the current diagram doesn't cover at all.

Honestly? I'd scrap everything you have done so far, start again with a blank slate, and reread your course notes / materials from the beginning, because you don't seem to have grasped much so far. And it's pretty important that you do understand the difference w=between serial and parallel, and even between an app and the act of creating an app - they are not the same thing.

Don't rush to code: think first and you will save yourself a lot of wasted time.
 
Share this answer
 
Comments
w4de 11-Jan-24 3:04am    
can you help me please? it's the last lab and it's due tomorrow((((
OriginalGriff 11-Jan-24 4:18am    
What do you need help with specifically?
w4de 11-Jan-24 4:27am    
with a flowchart to help
OriginalGriff 11-Jan-24 4:48am    
The problems with that are many: I don't have your (or indeed any) flowcharting software, and I don't have your assignment are but two biggies here.
But the biggest biggie is that this is your assignment: it's supposed to get you to learn what you are doing; how to use the information your teacher has imparted - and to start building your skill in processing that information to solve problems. Handing you a flowchart doesn't do that, any more than watching the Tour de France teaches you how to ride a bicycle - skills have to be used to improve, you can't get better at something just by looking at what an expert does you have to try it for yourself.

And all your existing flowchart shows is that you didn't understand what the expert (your teacher) was doing, or asking you to do. You have a day: start with a blank sheet ade your assignment and think about what you have to do. You'll get there if you try - good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900