cmake를 사용하여 .cpp 파일 및 .cu 파일을 포함하는 프로젝트를 컴파일하는 방법을 배우고 있습니다. 현재 main.cpp와 kernel.cu라는 두 개의 파일 만있는 장난감 예제로 작업하고 있습니다. 파일은cmake를 사용하여 cuda 파일을 cpp 파일에 올바르게 연결하는 방법
MAIN.CPP :
#include <stdlib.h>
#include <string.h>
extern void kernel_wrapper(int *a, int *b);
int main(int argc, char *argv[]){
int a = 2;
int b = 3;
printf("Input: a = %d, b = %d\n",a,b);
kernel_wrapper(&a, &b);
printf("Ran: a = %d, b = %d\n",a,b);
return 0;
}
kernel.cu 다음 cmake 파일이 게시물에서 영감을
//#include "cuPrintf.cu"
#include <stdio.h>
__global__ void kernel(int *a, int *b){
int tx = threadIdx.x;
// cuPrintf("tx = %d\n", tx);
switch(tx){
case 0:
*a = *a + 10;
break;
case 1:
*b = *b + 3;
break;
default:
break;
}
}
void kernel_wrapper(int *a, int *b){
// cudaPrintfInit();
//cuPrintf("Anything...?");
printf("Anything...?\n");
int *d_1, *d_2;
dim3 threads(2, 1);
dim3 blocks(1, 1);
cudaMalloc((void **)&d_1, sizeof(int));
cudaMalloc((void **)&d_2, sizeof(int));
cudaMemcpy(d_1, a, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_2, b, sizeof(int), cudaMemcpyHostToDevice);
kernel<<< blocks, threads >>>(d_1, d_2);
cudaMemcpy(a, d_1, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(b, d_2, sizeof(int), cudaMemcpyDeviceToHost);
printf("Output: a = %d\n", a[0]);
cudaFree(d_1);
cudaFree(d_2);
// cudaPrintfDisplay(stdout, true);
// cudaPrintfEnd();
}
: CMake script for CUDA 6.0 with C++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lpthread")
cmake_minimum_required(VERSION 3.2)
project(CUDAAndCP)
find_package(CUDA REQUIRED)
# For compilation ...
# Specify target & source files to compile it from
# Pass options to NVCC
set(
CUDA_NVCC_FLAGS
${CUDA_NVCC_FLAGS};
-O3 -gencode arch=compute_30,code=sm_30;
--std=c++11
)
cuda_add_library(kernel_obj kernel.cu)
cuda_add_executable(main main.cpp)
target_link_libraries(main ${CUDA_LIBRARIES})
target_link_libraries(main kernel_obj)
내가받을 수 있나요 컴파일 된 메인 바이너리 파일이지만 실행할 때 결과는
입니다. 대신Input: a = 2, b = 3
Anything...?
Output: a = 2
Ran: a = 12, b = 6
의 5,
Input: a = 2, b = 3
Anything...?
Output: a = 2
Ran: a = 2, b = 3
나는 그래서는 CUDA 파일이 기본 바이너리에 제대로 연결되지 않은 것 같다
g++ -c main.cpp
nvcc -c kernel.cu
nvcc -o main main.o kernel.o
을 실행하여 올바른 기본 바이너리 파일을 얻었다. 나는 왜 그리고 어떤 도움이 감사인지 정말로 이해하지 않는다!
우분투 14.04, 쿠다 7.5 및 cmake 3.2.0에서 실행 중입니다.
감사합니다. m.s. . kernel.cu에 int main() {}을 넣으려고했지만 이제는 단일 파일이기 때문에 링크 문제가 없어야합니다. 그러나 cuda_add_executable (main kernel.cu) 여전히 컴파일하고 여전히 잘못된 결과를 제공합니다. GPU 커널이 전혀 실행되지 않았습니다. 그러나 nvcc kernel.cu는 올바른 바이너리를 생성합니다. –
cmakeList.txt의 {nvcc ...} 섹션을 주석 처리하면 결과 메인이 정확합니다! 문제를 일으키는 원인은 누구나 알 수 있습니까? –