Infinite loop while finding prime number

Hello, i am trying to create parallel program to find prime numbers.

Here is my kernel file.

__kernel void IsPrime(__global int* A, __global int* B)
{
    //printf("chyba");
    
    int number = get_global_id(0);
    //printf("%d", number);
    //int number = stoi(A[global_id]);
    float result = 0;
    
    if (number > 2)
        result++;
   
    for (int i = 3; i <= number; i = i + 2) {
        bool addToPrime = true;
        for (int j = 3; j < i; j = j + 2) {
            if (i % j == 0) {
                addToPrime = false;
               
            }
        }
        if (addToPrime)
            result++;
   
    };
    printf("%d", result);
    
    //print("%d", result);
    B[0] = result;
    printf("%d", B);

    //printf("chyba");
};

and here is my prime.cpp file.

#pragma once
#define _CRT_SECURE_NO_DEPRECATE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include <CL/cl.h>

#define MAX_SOURCE_SIZE (0x100000)

int prime_parallel();
int seq_prime();


int main(int argc, char** argv)
{
	srand(clock());

	//seq_prime();

	prime_parallel();


	system("pause");
	return EXIT_SUCCESS;
}



int prime_parallel()
{
	printf("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/\n");
	printf("/~~~~~~~~~~~~~~~~~~ Parallel PRIME ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/\n");
	printf("/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/\n\n");


	FILE* fp;
	size_t source_size;
	char* source_str;

	fp = fopen("./prime.cl", "r");
	if (!fp) {
		fprintf(stderr, "\tFailed to load kernel.\n");
		exit(1);
	}
	source_str = (char*)malloc(MAX_SOURCE_SIZE);
	source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
	fclose(fp);

	int mem_size_A = sizeof(int);
	int* A = (int*)malloc(mem_size_A);

	int mem_size_B = sizeof(int);
	int* B = (int*)malloc(mem_size_B);


	int cislo = 100;//find prime numbers in 100
	A = &cislo;
	int cislo1 = 0;//set initial prime numbers
	B = &cislo1;

	printf("\tInitializing OpenCL\n");

	cl_platform_id platform_id = NULL;
	cl_device_id device_id = NULL;

	clGetPlatformIDs(1, &platform_id, NULL);

	clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL);

	// Vytvorenie kontextu
	cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, NULL);

	cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, NULL);



	// Vytvorenie programu
	cl_program program = clCreateProgramWithSource(context, 1, (const char**)&source_str, (const size_t*)&source_size, NULL);
	clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);

	// Vytvorenie kernelu s programom//CREATE KERNEL
	cl_kernel kernel = clCreateKernel(program, "IsPrime", NULL);


	cl_mem device_memory_B = clCreateBuffer(context, CL_MEM_READ_WRITE, mem_size_B, NULL, NULL);
	cl_mem device_memory_A = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, mem_size_A, A, NULL);



	size_t global[2];

	clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&device_memory_A);
	clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&device_memory_B);

	//global[0] = 1000;


	printf("\tSTARTED: Parallel prime\n");
	clock_t start_time = clock();

	//spustenie kernelu//start kernel
	clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global, NULL, 0, NULL, NULL);
	//nacitanie//load
	clEnqueueReadBuffer(command_queue, device_memory_B, CL_TRUE, 0, mem_size_B, B, 0, NULL, NULL);

	// Koniec vypoctu
	clock_t end_time = clock();
	clock_t duration = end_time - start_time;

	printf("\tFINISHED: Parallel prime (Duration: %f seconds)\n\n", (float)duration / CLOCKS_PER_SEC);
	free(A);
	free(B);
	clFinish(command_queue);
	clReleaseMemObject(device_memory_A);
	clReleaseMemObject(device_memory_B);


	clReleaseProgram(program);
	clReleaseKernel(kernel);
	clReleaseCommandQueue(command_queue);
	clReleaseContext(context);

	return EXIT_SUCCESS;
}

So I think that my kernel file is good I am not sure if my assign to B[0]= result is good, i was trying B[get_global_id[1], but then i get error generated so in this i am not sure.
In my cpp file i am not understand everything to detail working with memory is little confusing for me. But i was trying to to change global work and local. So i dont know if i way how i alocate memory is good.
where i am making mistake?

Please see:

In particular, Posting Guideline #4.

Here, you’ve basically just said it doesn’t work; please fix it for me.

Have you tried to fix this? What have you tried? What were the results?

I already try to fix this. And i add some option that i try.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.