一个完整的手工构建的cuda动态链接库工程 02记

step3, 两个 API 函数的动态链接库 Makefile 版本

对比之前的文件树:

现在的文件树:

添加了3个新文件,修改了4个旧文件,其中include/ic_add.h 其实可以改成 icmm.h,作为整个 shared library 被调用的头文件。

现将新文件和更改的文件罗列如下:

gpu/sub.cu 

#include <stdio.h>
#include <cuda_runtime.h>

__global__ void vector_sub_kernel(float *A, float *B, float *C, int n)
{
  int i = blockDim.x * blockIdx.x + threadIdx.x;

  if (i < n)
  {
    C[i] = A[i] - B[i] + 0.0f;
  }
}

extern "C"  void  vector_sub_gpu(float *A, float *B, float *C, int n)
{
  dim3 grid, block;

  block.x = 256;
  grid.x = (n + block.x - 1) / block.x;
  printf("CUDA kernel launch with %d blocks of %d threads\n", grid.x, block.x);

  vector_sub_kernel<<<grid, block>>>(A, B, C, n);
}

gpu/sub.h

#pragma once

extern "C"  void  vector_sub_gpu(float *A, float *B, float *C, int n);

src/ic_sub.cpp

#include <stdio.h>
#include <cuda_runtime.h>
#include "gpu/sub.h"
//extern void vector_add_gpu(float *A, float *B, float *C, int n);

void ic_sub(float* A, float* B, float *C, int n)
{
  vector_sub_gpu(A, B, C, n);
}

include/ic_add.h


#pragma once
#include<cuda_runtime.h>

void hello_print();
void ic_add(float* A, float* B, float *C, int n);
void ic_sub(float* A, float* B, float *C, int n);

Makefile

#libicmm.so

TARGETS = libicmm.so
GPU_ARCH= -arch=sm_70


all: $(TARGETS)



sub.o: gpu/sub.cu
	nvcc    -Xcompiler -fPIC $(GPU_ARCH) -c $<

add.o: gpu/add.cu
	nvcc    -Xcompiler -fPIC $(GPU_ARCH) -c $<
#-dc
#-rdc=true

add_link.o: add.o
	nvcc   -Xcompiler -fPIC  $(GPU_ARCH) -dlink   -o $@  $<  -L/usr/local/cuda/lib64 -lcudart -lcudadevrt

ic_add.o: src/ic_add.cpp
	g++ -fPIC -c $<  -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -lcudart -lcudadevrt -I./

ic_sub.o: src/ic_sub.cpp
	g++ -fPIC -c $<  -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -lcudart -lcudadevrt -I./

$(TARGETS): sub.o ic_sub.o add.o ic_add.o add_link.o
	mkdir -p lib
	g++ -shared -fPIC  $^  -o lib/libicmm.so -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcudart -lcudadevrt 
	-rm -f *.o


.PHONY:clean
clean:
	-rm -f *.o lib/*.so test ./bin/test
	-rm -rf lib bin

makefile_bin

# executable
TARGET = test
GPU_ARCH = -arch=sm_70

all: $(TARGET)

add.o: gpu/add.cu
	nvcc -dc -rdc=true $(GPU_ARCH) -c $<

sub.o: gpu/sub.cu
	nvcc -dc -rdc=true $(GPU_ARCH) -c $<

add_link.o: add.o
	nvcc $(GPU_ARCH) -dlink   -o $@  $<  -L/usr/local/cuda/lib64 -lcudart -lcudadevrt

sub_link.o: sub.o
	nvcc $(GPU_ARCH) -dlink   -o $@  $<  -L/usr/local/cuda/lib64 -lcudart -lcudadevrt

ic_add.o: src/ic_add.cpp
	g++ -c $<  -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -lcudart -lcudadevrt -I./

ic_sub.o: src/ic_sub.cpp
	g++ -c $<  -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -lcudart -lcudadevrt -I./

test.o: testing/test.cpp
	g++ -c $< -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcudart -lcudadevrt -I./include

test: sub.o ic_sub.o sub_link.o add.o ic_add.o test.o add_link.o
	g++ $^ -L/usr/local/cuda/lib64 -lcudart -lcudadevrt   -o test
	mkdir ./bin
	cp ./test ./bin/
	-rm -f *.o

.PHONY:clean
clean:
	-rm -f *.o bin/* $(TARGET)

testing/Makefile

#test

TARGET = test

all: $(TARGET)

CXX_FLAGS = -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcudart -lcudadevrt  -I../include -L../

test.o: test.cpp
	g++  -c $< $(CXX_FLAGS)

$(TARGET):test.o
	g++ $< -o $@ -L/usr/local/cuda/lib64 -lcudart -lcudadevrt  -L../lib  -licmm
	@echo "to execute: export LD_LIBRARY_PATH=${PWD}/../lib"

.PHONY:clean
clean:
	-rm -f *.o $(TARGET)

testing/test.cpp

#include "ic_add.h"
#include <cuda_runtime.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)
{
  int n = 50;
  size_t size = n * sizeof(float);

  float *h_A = (float *)malloc(size);
  float *h_B = (float *)malloc(size);
  float *h_C = (float *)malloc(size);

  for (int i = 0; i < n; ++i)
  {
    h_A[i] =  rand() / (float)RAND_MAX;
    h_B[i] =  rand() / (float)RAND_MAX;
  }

  float *d_A = NULL;
  float *d_B = NULL;
  float *d_C = NULL;

  cudaMalloc((void **)&d_A, size);
  cudaMalloc((void **)&d_B, size);
  cudaMalloc((void **)&d_C, size);
  cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
  cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);
/*
  int threadsPerBlock = 256;
  int blocksPerGrid = (n + threadsPerBlock - 1) / threadsPerBlock;
  printf("CUDA kernel launch with %d blocks of %d threads\n", blocksPerGrid, threadsPerBlock);

  vector_add_kernel<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, n);
*/

  ic_sub(d_A, d_B, d_C, n);

  printf("Copy output data from the CUDA device to the host memory\n");
  cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);

  for (int i = 0; i < n; ++i)
  {
    printf("%3.2f ", h_C[i]);
    // if (fabs(h_A[i] + h_B[i] - h_C[i]) > 1e-5) {      fprintf(stderr, "Result verification failed at element %d!\n", i);      exit(EXIT_FAILURE);    }
  }

  printf("\nTest PASSED\n");

  cudaFree(d_A);
  cudaFree(d_B);
  cudaFree(d_C);
  free(h_A);
  free(h_B);
  free(h_C);

  printf("Done\n");
  return 0;
}

其中在 test.cpp中调用了新的api函数 ic_sub(...)

step4,  将Makefile 转换成 cmake 自定义版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值