Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat t592.cu
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <thread>
- #include <mutex>
- #include <iostream>
- #include <unistd.h>
- std::mutex mutexCudaExecution;
- __global__ void testCuda() {}
- void runCuda()
- {
- testCuda<<<1, 1>>>();
- cudaDeviceSynchronize();
- }
- void wrapperLock()
- {
- std::lock_guard<std::mutex> lock(mutexCudaExecution);
- // changing this value to 20000 does NOT trigger "Segmentation fault"
- usleep(5000);
- runCuda();
- }
- // The thread configuration structure.
- typedef struct
- {
- int device;
- pthread_t thread;
- cudaError_t status;
- }
- config_t;
- // The function executed by each thread assigned with CUDA device.
- void *thread_func(void *arg)
- {
- // Unpack the config structure.
- config_t *config = (config_t *)arg;
- int device = config->device;
- pthread_t my_thread=config->thread;
- cudaError_t cuda_status = cudaSuccess;
- cuda_status = cudaSetDevice(device);
- if (cuda_status != cudaSuccess) {
- fprintf(stderr, "Cannot set focus to device %d, status = %d\n",
- device, cuda_status);
- config->status = cuda_status;
- pthread_exit(NULL);
- }
- printf("thread %d initialized\n", my_thread);
- wrapperLock();
- printf("thread %d deinitialized\n", my_thread);
- config->status = cudaSuccess;
- return NULL;
- }
- int main(int argc, char* argv[])
- {
- int nthreads = 25;
- // Create workers configs. Its data will be passed as
- // argument to thread_func.
- config_t* configs = (config_t*)malloc(sizeof(config_t) * nthreads);
- // For each CUDA device found create a separate thread
- // and execute the thread_func.
- for (int i = 0; i < nthreads; i++) {
- config_t *config = configs + i;
- config->device = 0;
- //config->in_host = in + np * i;
- int status = pthread_create(&config->thread, NULL, thread_func, config);
- if (status) {
- fprintf(stderr, "Cannot create thread for device %d, status = %d\n",
- i, status);
- return status;
- }
- }
- // Wait for device threads completion.
- // Check error status.
- int status = 0;
- for (int i = 0; i < nthreads; i++) {
- pthread_join(configs[i].thread, NULL);
- status += configs[i].status;
- }
- if (status)
- return status;
- free(configs);
- return 0;
- }
- $ nvcc -std=c++11 -arch=sm_20 -o t592 t592.cu
- $ cuda-memcheck ./t592
- ========= CUDA-MEMCHECK
- thread 1093793536 initialized
- thread 1068615424 initialized
- thread 1085400832 initialized
- thread 1077008128 initialized
- thread 1060222720 initialized
- thread 1051830016 initialized
- thread 1102186240 initialized
- thread 951117568 initialized
- thread 934332160 initialized
- thread 1018259200 initialized
- thread 900761344 initialized
- thread 1009866496 initialized
- thread 1001473792 initialized
- thread 993081088 initialized
- thread 984688384 initialized
- thread 976295680 initialized
- thread 967902976 initialized
- thread 959510272 initialized
- thread 942724864 initialized
- thread 1035044608 initialized
- thread 1043437312 initialized
- thread 925939456 initialized
- thread 917546752 initialized
- thread 909154048 initialized
- thread 1026651904 initialized
- thread 1093793536 deinitialized
- thread 1068615424 deinitialized
- thread 1085400832 deinitialized
- thread 1077008128 deinitialized
- thread 1060222720 deinitialized
- thread 1051830016 deinitialized
- thread 1102186240 deinitialized
- thread 951117568 deinitialized
- thread 934332160 deinitialized
- thread 1018259200 deinitialized
- thread 900761344 deinitialized
- thread 1009866496 deinitialized
- thread 1001473792 deinitialized
- thread 993081088 deinitialized
- thread 984688384 deinitialized
- thread 976295680 deinitialized
- thread 967902976 deinitialized
- thread 959510272 deinitialized
- thread 942724864 deinitialized
- thread 1035044608 deinitialized
- thread 1043437312 deinitialized
- thread 925939456 deinitialized
- thread 917546752 deinitialized
- thread 909154048 deinitialized
- thread 1026651904 deinitialized
- ========= ERROR SUMMARY: 0 errors
- $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement