Monday 25 May 2015

CUDA Multi-GPU : To set a GPU of required Compute Capability as current GPU

              Suppose, your system has multiple CUDA enabled GPUs. Only one GPU can be set as current GPU. In this situation, you can set a particular GPU having specific compute capability(say 1.2) as a current GPU.
              There are two ways to achieve the same as follows:



1st way: Find out the total number of GPUs on your system and compare their major and minor with required major and minor (e.g. major==1 and minor==2). If you get such GPU, then set it as current GPU.

2nd way: Create a structure variable of predefined structure cudaDeviceProp (say p). Set the required major and minor values for p (e.g. p.major=1 and p.minor=2). Use predefined function cudaChooseDevice() to find the id of GPU whose compute capability is closer to p. Then set this GPU as current GPU. Keep in mind, cudaChooseDevice() function finds an id of GPU whose configuration is more closer (not necessary to be same) to required configuration from the available GPUs. 

              Let us see, the program for each way.

1st way: CUDA program to set a GPU of compute capability 1.2 as current GPU.
#include<stdio.h>
#include<cuda.h>

int main()
{
cudaDeviceProp p;
int count,i,flag=0;

cudaGetDeviceCount(&count);

for(i=0;i<count;i++)
{
    cudaGetDeviceProperties(&p,i);


        if(p.major==1 && p.minor==2)
        {
            cudaSetDevice(i);
            printf("GPU with Compute Capability 1.2 is set as current GPU on your system.\n");
            flag=1;
        }


}

if(flag==0)
{
    printf("GPU of Compute Capabilty 1.2 is not available on your system.\n");
}


}


Output:
GPU with Compute Capability 1.2 is set as current GPU on your system.


2nd way: CUDA program to set a GPU as current GPU whose compute capability is more closer to 1.2 by using cudaChooseDevice().
#include<stdio.h>
#include<cuda.h>

int main()
{
    cudaDeviceProp p;
    int dev;

    p.major=1;
    p.minor=2;

    cudaChooseDevice(&dev,&p);

    cudaSetDevice(dev);

    printf("GPU with Compute Capability closer to 1.2 is set as current GPU on your system.\n");
}


Output:
GPU with Compute Capability closer to 1.2 is set as current GPU on your system.




1 comment:

  1. Very Nice Article.. I found this article useful too.
    http://www.cstechera.in/2015/05/what-is-cuda.html

    ReplyDelete