Explorar o código

Fitting CMake and open/close PCI context

mathiasb %!s(int64=6) %!d(string=hai) anos
pai
achega
c7198accdc
Modificáronse 4 ficheiros con 50 adicións e 24 borrados
  1. 1 1
      CMakeLists.txt
  2. 2 0
      include/kernels.h
  3. 25 0
      src/kernels.cu
  4. 22 23
      src/main.cu

+ 1 - 1
CMakeLists.txt

@@ -23,4 +23,4 @@ cuda_add_executable(gpufirstcomm
   src/memcpy_sse.c
   src/kernels.cu)
 
-target_link_libraries(gpufirstcomm cuda)
+target_link_libraries(gpufirstcomm cuda pcilib)

+ 2 - 0
include/kernels.h

@@ -1,6 +1,8 @@
 #include "cuda.h"
 #include "cuda_runtime_api.h"
+#include "gdrapi.h"
 
 __device__ void add_two_device(CUdeviceptr number);
 __global__ void add_three_global(CUdeviceptr number);
 __global__ void add_one_global(CUdeviceptr number);
+void firstest(CUdeviceptr dptr, void* va);

+ 25 - 0
src/kernels.cu

@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "kernels.h"
+#include "common.h"
 
 __device__
 void add_two_device(CUdeviceptr number)
@@ -22,3 +23,27 @@ void add_one_global(CUdeviceptr number)
 {
     (* (int*) number)++;
 }
+
+void first_test(CUdeviceptr dptr, void* va)
+{
+    int set, get;
+    printf("Use the nvidia api\n");
+    set = 4242;
+    get = 0;
+    printf("set = %d\nget = %d\n",set,get);
+    assert_cu( cuMemcpyHtoD(dptr,&set,sizeof(set)) );
+    add_three_global<<< 1,1 >>>(dptr);
+    cudaDeviceSynchronize();
+    assert_cu( cuMemcpyDtoH(&get,dptr,sizeof(get)) );
+    printf("set = %d\nget = %d\n",set,get);
+
+    printf("Use the gdr api\n");
+    set = 4242;
+    get = 0;
+    printf("set = %d\nget = %d\nva = %d\n",set,get,*(int*) va);
+    assert_gdr( gdr_copy_to_bar(va,&set,sizeof(set)) );
+    add_one_global<<< 1,1 >>>(dptr);
+    cudaDeviceSynchronize();
+    assert_gdr( gdr_copy_from_bar(&get,va,sizeof(get)) );
+    printf("set = %d\nget = %d\nva = %d\n",set,get,*(int*) va);
+}

+ 22 - 23
src/main.cu

@@ -3,9 +3,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "common.h"
-/*#include "cuda.h"*/
 #include "kernels.h"
 #include "gdrapi.h"
+#include "pcilib.h"
 
 int main()
 {
@@ -36,8 +36,8 @@ int main()
     /* Check context */
     assert_cu( cuCtxGetDevice(&GPU) );
     printf("Device for this context: %d\n",GPU);
-    CUcontext ctx;
-    assert_cu( cuCtxCreate(&ctx,0,GPU) );
+    CUcontext cuCtx;
+    assert_cu( cuCtxCreate(&cuCtx,0,GPU) );
     assert_cu( cuCtxGetDevice(&GPU) );
     printf("Device for this context: %d\n",GPU);
     
@@ -61,27 +61,26 @@ int main()
     
     /* At this point the GPU's mem is mapped to a CPU buffer to enable DMA */
 
-    int set, get;
-    printf("Use the nvidia api\n");
-    set = 4242;
-    get = 0;
-    printf("set = %d\nget = %d\n",set,get);
-    assert_cu( cuMemcpyHtoD(dptr,&set,sizeof(set)) );
-    add_three_global<<< 1,1 >>>(dptr);
-    cudaDeviceSynchronize();
-    assert_cu( cuMemcpyDtoH(&get,dptr,sizeof(get)) );
-    printf("set = %d\nget = %d\n",set,get);
-
-    printf("Use the gdr api\n");
-    set = 4242;
-    get = 0;
-    printf("set = %d\nget = %d\nva = %d\n",set,get,*(int*) va);
-    assert_gdr( gdr_copy_to_bar(va,&set,sizeof(set)) );
-    add_one_global<<< 1,1 >>>(dptr);
-    cudaDeviceSynchronize();
-    assert_gdr( gdr_copy_from_bar(&get,va,sizeof(get)) );
-    printf("set = %d\nget = %d\nva = %d\n",set,get,*(int*) va);
+    /* PCI */
+    pcilib_t* pciCtx;
+    char* pciVa;
+    pciCtx = pcilib_open("/dev/fpga0",NULL);
+    if( pciCtx == NULL )
+    {
+	printf("Cannot open a context for pci\n");
+	exit( EXIT_FAILURE );
+    }
 
+    pciVa = pcilib_resolve_bar_address(pciCtx,PCILIB_BAR_DETECT, 0);
+    if( pciVa == NULL )
+    {
+	printf("Cannot resolve PCI physical adress to virtual\n");
+	exit( EXIT_FAILURE );
+    }
+    
+    pcilib_close(pciCtx);
+    
+    
     
     /* Close everything */
     assert_gdr( gdr_unmap(g,GPUMemHandle,va,(size_t) GPUProp.sharedMemPerBlock) );