First cuda program on Ubuntu

Hi guys,

After one day I manage to install cuda and sdk on Ubunt 9.04.
Everithing works fine, I can compile and execute all the sdk examples.

I tried to write and compile an example found in this forum, but the result isn’t as waited.
In the code below the result should be the square of the element of the vector, but changing or not the
row in which the calculation is done (the original is commented) in the kernel the result is always the same:
0 0.000000
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
6 6.000000
7 7.000000
8 8.000000
9 9.000000
Apparently the cudamemcpy isn’t working.
I tried also another very simple example on matrix addition and the patology is exactly the same.
Do you have some suggestions?

thx,

g.

code:

#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>

// Kernel that executes on the CUDA device
global void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
//if (idx<N) a[idx] = a[idx] * a[idx];
if (idx<N) a[idx] = 2.;
}

// main routine that executes on the host
int main(void)
{
float *a_h, *a_d; // Pointer to host & device arrays
const int N = 10; // Number of elements in arrays
size_t size = N * sizeof(float);
a_h = (float *)malloc(size); // Allocate array on host
cudaMalloc((void **) &a_d, size); // Allocate array on device
// Initialize host array and copy it to CUDA device
for (int i=0; i<N; i++) a_h[i] = (float)i;
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
// Do calculation on device:
int block_size = 4;
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
square_array <<< n_blocks, block_size >>> (a_d, N);
// Retrieve result from device and store it in host array
cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
// Print results
for (int i=0; i<N; i++) printf("%d %f\n", i, a_h[i]);
// Cleanup
free(a_h); cudaFree(a_d);
}

I don’t know what the problem is. Works correctly on my laptop. It’s returning the value 2 for each thread.

N.

Thx a lot for your reply…

probably is due to the installation (but this is very strange because all the sdk projects work properly)…

Have you used only:

nvcc namefile.cu

to compile?

thx

G.

Yep :)