hi guys
I hope to post in the correct area!
I’m trying to write a simple program to understando how CUDA context works:
this is my program:
#include <pthread.h>
#include <stdio.h>
#include <cuda.h>
#define NUM_THREADS 2
float d1, d2;
float * m1, * m2;
int devnumber = 1;
CUcontext hcuContext = 0;
void *
inizialize (void *)
{
CUdevice hcuDevice;
cuDeviceGet( &hcuDevice, devnumber );
cuCtxCreate( &hcuContext, 0, hcuDevice );
cudaMalloc ((void **) &m1, sizeof (float));
cudaMalloc ((void **) &m2, sizeof (float));
float dd1 = 1.0;
float dd2 = 2.0;
cudaMemcpy (m1, &dd1, sizeof (float), cudaMemcpyHostToDevice);
cudaMemcpy (m2, &dd2, sizeof (float), cudaMemcpyHostToDevice);
//cudaMemcpy (&d1, m1, sizeof (float), cudaMemcpyDeviceToHost);
//cudaMemcpy (&d2, m2, sizeof (float), cudaMemcpyDeviceToHost);
//fprintf (stdout, “%f %f \n”, d1, d2);
cuCtxPopCurrent(&hcuContext);
cudaThreadSynchronize ();
pthread_exit (NULL);
}
void *
compute_function (void *)
{
cuCtxPushCurrent( hcuContext );
cudaMemcpy (&d1, m1, sizeof (float), cudaMemcpyDeviceToHost);
cudaMemcpy (&d2, m2, sizeof (float), cudaMemcpyDeviceToHost);
fprintf (stdout, “%f %f \n”, d1, d2);
cudaThreadSynchronize ();
pthread_exit (NULL);
}
int
main (int argc, char *argv)
{
pthread_t threads;
pthread_create (&threads, NULL, inizialize, NULL);
if (pthread_join (threads, NULL))
{
fprintf (stderr, “error pthread_join\n”);
return EXIT_FAILURE;
}
pthread_create (&threads, NULL, compute_function, NULL);
if (pthread_join (threads, NULL))
{
fprintf (stderr, “error pthread_join\n”);
return EXIT_FAILURE;
}
cuCtxDestroy(hcuContext);
return EXIT_SUCCESS;
}
is corret to use the context in this way? I need that the second thread print 1.0 and 2.0 but without cuda context doesn’t work. With this solution the compiler return the following errors:
/tmp/tmpxft_00006a46_00000000-12_th1.o: In function main': tmpxft_00006a46_00000000-1_th1.cudafe1.cpp:(.text+0x10a5c): undefined reference to
cuCtxDestroy’
/tmp/tmpxft_00006a46_00000000-12_th1.o: In function compute_function(void*)': tmpxft_00006a46_00000000-1_th1.cudafe1.cpp:(.text+0x10a80): undefined reference to
cuCtxPushCurrent’
/tmp/tmpxft_00006a46_00000000-12_th1.o: In function inizialize(void*)': tmpxft_00006a46_00000000-1_th1.cudafe1.cpp:(.text+0x10b12): undefined reference to
cuDeviceGet’
tmpxft_00006a46_00000000-1_th1.cudafe1.cpp:(.text+0x10b24): undefined reference to cuCtxCreate' tmpxft_00006a46_00000000-1_th1.cudafe1.cpp:(.text+0x10b90): undefined reference to
cuCtxPopCurrent’
someone can give me an hand? Please i need it works for my degree thesis