Practice CUDA runtime API(1) –DeviceManagementRT

來源:互聯網
上載者:User

Today I write a simple test to call CUDA RuntimeAPI.

1 DeviceManagement RT

 

 1.1 cudaGetDeviceCount

 cudaError_t cudaGetDeviceCount(int *count)

 use this function to get the number of devices with compute capability

 

 1.2 cudaGetDevice

 cudaError_t cudaGetDeviceC(int *dev)

 use this function to get device number which is currently being used

 

 1.3 cudaGetDeviceProperties

 cudaError_t cudaGetDeviceProperties(struct cudaDeviceProp *prop, int dev)

 use this function to get the detail properties of dev device, you will know how large the GlobalMemory it has, how many register per block, etc.

 

 1.4 cudaChooseDevice

 cudaError_t cudaChooseDevice(int *dev, const struct cudaDeviceProp *prop)

 returns in *dev the device which properties best match *prop, if you have many devices, you can choose them by setting *prop, then get the device number.

 

 1.5 cudaSetDevice

 cudaError_t cudaSetDevice(int dev)

 after you choose the device you want to use, set the device number and call this function, then the specify one will work for you.

 

At last I post the code which can get all the CUDA device prop in you computer

/*<br /> * Copyright (c) 2009, SCUT Chenhu_lab<br /> * All rights reserved.<br /> *<br /> * filename:cudart_device.cu<br /> * content:test runtime api of cuda<br /> *<br /> * current version:1.0<br /> * auther: Jiangfeng Peng<br /> * finish date:2009-6-4<br /> *<br /> * old version: null<br /> * ori auther: null<br /> * finish daa: null<br /> */</p><p>#include <stdio.h><br />#include <stdlib.h><br />#include <cuda_runtime.h></p><p>bool InitCUDA()<br />{<br />int count;<br />int i,j;<br />cudaDeviceProp prop;</p><p>/* cudaError_t cudaGetDeviceCount(int *count) -- returns the number of compute-capable devices */<br />cudaGetDeviceCount(&count);</p><p>if (count == 0)<br />{<br />fprintf(stderr, "There is no device./n");<br />return false;<br />}<br />else<br />{<br />printf("/ncudaGetdeviceCount: %d/n/n", count);<br />}</p><p>for (i=0,j=0; i < count; i++)<br />{<br />/* cudaError_t cudaGetDeviceProperties(struct cudaDeviceProp *prop, int dev)<br />-- returns information on the compute-device */<br />if (cudaGetDeviceProperties(&prop, i) == cudaSuccess)<br />{<br />printf("Num %d cudaDeviceProp :/n", i);<br />printf( " name %s /n totalGlobalMem %dB = %1.2fMB/n sharedMemPerBlock %dB = %dKB /n regsPerBlock %d = %dk/n"<br />" warpSize %d /n memPitch %dB = %dKB/n maxThreadsPerBlock %d /n maxThreadsDim: x %d y %d z %d /n"<br />" maxGridSize: x %d y %d z %d /n totalConstMem %dB = %dKB /n major %d /n minor %d /n clockRate %dKHZ = %1.2fGHZ /n"<br />" textureAlignment %d /n deviceOverlap %d /n multiProcessorCount %d /n/n",<br />prop.name, prop.totalGlobalMem, (double)prop.totalGlobalMem/1024/1024, prop.sharedMemPerBlock, prop.sharedMemPerBlock/1024, prop.regsPerBlock,<br />prop.regsPerBlock/1024, prop.warpSize,prop.memPitch, prop.memPitch/1024, prop.maxThreadsPerBlock, prop.maxThreadsDim[0], prop.maxThreadsDim[1],<br />prop.maxThreadsDim[2], prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2], prop.totalConstMem, prop.totalConstMem/1024, prop.major,<br />prop.minor, prop.clockRate, (double)prop.clockRate/1024/1024, prop.textureAlignment, prop.deviceOverlap, prop.multiProcessorCount);</p><p>if (prop.major >= 1)<br />{<br />j = 1;<br />}<br />}<br />}</p><p>if(j == 0)<br />{<br />fprintf(stderr, "There is no device supporting CUDA 1.x./n");<br />return false;<br />}</p><p>cudaSetDevice(i);</p><p>return true;<br />}</p><p>int main()<br />{<br />if(!InitCUDA())<br />{<br />return 0;<br />}</p><p>printf("CUDA initialized./n");</p><p>return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.