Anonymní profil Matus – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Matus – Programujte.comAnonymní profil Matus – Programujte.com

 

Příspěvky odeslané z IP adresy 185.23.113.–

Matus
C / C++ › Infinite loop v OpenCL
10. 3. 2023   #391140

Dobry den, snazim sa naprogramovat jednoduche hladanie prvocisiel v opencl, avsak dostavanm infinite loop? neviete kde robim chybu?

 to je moj 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");
};

a tu je moj prime.cpp subor:

#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;
	A = &cislo;
	int cislo1 = 0;
	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);
	/////////////////////
	/*int out_put_size = sizeof(int);
	cl_mem device_memory_C = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int), NULL, NULL);
	int mem_size_output = sizeof(int);
	int* output = (int*)malloc(mem_size_output);
	int param = KEY;*/
	//int outp = OUTPUT;


	// 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
	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
	clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global, NULL, 0, NULL, NULL);
	//nacitanie
	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;
}



 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý