Pregunta

I'm trying to compile a C program to try parallel programming, when I try to compile it with nvcc compiler (Nvidia) it gives me those errors:

inicis.cu(3): error: attribute "global" does not apply here

inicis.cu(3): error: incomplete type is not allowed

inicis.cu(3): error: identifier "a" is undefined

inicis.cu(3): error: expected a ")"

inicis.cu(4): error: expected a ";"

/usr/include/_locale.h(68): error: expected a declaration

inicis.cu(20): error: type name is not allowed

inicis.cu(21): error: type name is not allowed

inicis.cu(22): error: type name is not allowed

inicis.cu(41): error: identifier "dev_a" is undefined

inicis.cu(42): error: identifier "dev_b" is undefined

inicis.cu(43): error: identifier "dev_c" is undefined

It seems nvcc doesn't recognize the global attribute made by Nvidia...

Here's my C program, it's pretty simple:

__global__ void operate(*memoria1, *memoria2)
{
    memoria2[threadIdx.x] = memoria1[threadIdx.x] + 1;
}


int main(int args, char **argv){

    int a[5], c[5];
    int *memory_1, *memory_2;

    cudaMalloc(void** &memory_1, 5 * sizeof(int));
    cudaMalloc(void** &memory_2, 5 * sizeof(int));

    cudaMemcpy(memory_1, a, 5 * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(memory_2, c, 5 * sizeof(int), cudaMemcpyHostToDevice);

    operate <<<1, 5>>>(memory_1, memory_2);

    cudaMemcpy(c, memory_2, 5 * sizeof(int), cudaMemcpyDeviceToHost);

    for (int i = 0; i < sizeof(c); ++i)
    {
        printf ("%d" , c[i]);
    }

    cudaFree(memory_1);
    cudaFree(memory_2);

    return 0;
}

I think it could be the compiler but what do you think It would be?

¿Fue útil?

Solución

I think if you make these changes:

__global__ void operate(int* memoria1, int* memoria2)
                         ^              ^

and:

cudaMalloc((void**) &memory_1, 5 * sizeof(int));
cudaMalloc((void**) &memory_2, 5 * sizeof(int));
           ^      ^

Your code will compile and run properly. Your results will be kind of wierd since the code doesn't actually initialize the values of a and c that are being operated on by the CUDA kernel. So you might want to initialize those.

Otros consejos

TL:DR Your kernel declaration is in the wrong place and the compiler is treating it as a variable instead. You need to make sure your kernel declaration is on a .cu file where it's being used. Not 100 if it need to be first declared in a file that uses it but just mess around with it's declaration spot.

I was having this same exact issue on my cuda code. The issue was that I was declaring my kernel in the wrong place. I'll elaborate

I had a header file for my main called host.h where I was keeping all the headers I was using as well as declaration for my project methods. Here I was also adding the header files for cuda runtime as well as my device.h file where I was declaring my kernel.

Looked something like this:

host.h

#include "iostream" #include "iomanip"

#include "device.h"

device.h

global myCudeKernel();

And well this was causing problems for some reason. Ended up moving the device.h header to the main file where it's used and no issue.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top