Mac中用clang++和nvcc编译cuda程序的一个例子
2015-03-12 02:11
393 查看
主函数在main.cpp中,用clang++编译,cuda函数放在KernelWrapper.cu中,用nvcc编译。另外main.cpp中需要包含头文件KernelWrapper.h
KernelWrapper.h
KernelWrapper.cu
makefile
KernelWrapper.h
#ifndef _KernelWrapper_h #define _KernelWrapper_h void RunTest(); #endif
KernelWrapper.cu
#include <stdio.h> #include "KernelWrapper.h" __global__ void TestDevice(int *deviceArray) { int idx = blockIdx.x*blockDim.x + threadIdx.x; deviceArray[idx] = deviceArray[idx]*deviceArray[idx]; } void RunTest() { int* hostArray; int* deviceArray; const int arrayLength = 16; const unsigned int memSize = sizeof(int) * arrayLength; hostArray = (int*)malloc(memSize); cudaMalloc((void**) &deviceArray, memSize); printf("Init Data\n"); for(int i=0;i<arrayLength;i++) { hostArray[i] = i+1; printf("%d\n", hostArray[i]); } cudaMemcpy(deviceArray, hostArray, memSize, cudaMemcpyHostToDevice); TestDevice <<< 4, 4 >>> (deviceArray); cudaMemcpy(hostArray, deviceArray, memSize, cudaMemcpyDeviceToHost); printf("After Kernel Function\n"); for(int i=0;i<arrayLength;i++) { printf("%d\n", hostArray[i]); } cudaFree(deviceArray); free(hostArray); printf("done"); }main.cpp
#include "KernelWrapper.h" int main( int argc, char** argv) { RunTest(); return 0; }
makefile
all: program program: KernelWrapper.o main.o clang++ -o program -L/usr/local/cuda/lib -lcuda -lcudart KernelWrapper.o main.o KernelWrapper.o:KernelWrapper.cu /usr/local/cuda/bin/nvcc -c -arch=sm_20 KernelWrapper.cu main.o:main.cpp clang++ -c main.cpp clean: rm -f *.o program
相关文章推荐
- 在MAC OS X 10.9下编译CUDA 5.5例子程序出错解决方案
- cuda8.0使用nvcc编译程序出现warning:The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated的解决办法
- Mac OS X下编译CUDA6例子时出现关于switch的warning的解决办法
- MinGW Developer Studio 中编译dev cpp程序(一个opengl例子)
- Mac OS X: Launchd执行程序的一个例子
- Mac OS X: Launchd执行程序的一个例子
- 使用CMake编译QT程序的一个例子(没有使用Qt特殊class)
- NVCC 分歩 编译 CUDA 程序
- 【Linux/C】程序的编译和链接--例子
- 如何反编译一个android程序一
- 但每次进行编译就有一个问题 编的程序是正确的 我才学编程
- gcc程序编译时添加头文件和库文件时需注意的一个地方
- 在编译CUDA程序时出现 fatal error C1083: 无法打开包括文件:“cutil.h”: No such file or directory 的问题
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- 在浏览器中 编译php程序例子时,老是报NOTICE错误
- 一个简单C程序的编译及反汇编
- 把一个VC程序,设为Unicode的编译
- ubuntu下使用eclipse调用libpcap库编写一个小程序时出的错误:pcap.h没有参与编译
- Mac/Linux编译OpenCV程序
- 把桌面qt代码编译运行到qpe上的一个例子