]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | First invocation: NVIDIA CUBLAS library API | |
7 | ******************************************* | |
8 | ||
9 | In this first use case (see below), a function in the CUBLAS library is called | |
10 | prior to any of the functions in the OpenACC library. More specifically, the | |
11 | function ``cublasCreate()``. | |
12 | ||
13 | When invoked, the function initializes the library and allocates the | |
14 | hardware resources on the host and the device on behalf of the caller. Once | |
15 | the initialization and allocation has completed, a handle is returned to the | |
16 | caller. The OpenACC library also requires initialization and allocation of | |
17 | hardware resources. Since the CUBLAS library has already allocated the | |
18 | hardware resources for the device, all that is left to do is to initialize | |
19 | the OpenACC library and acquire the hardware resources on the host. | |
20 | ||
21 | Prior to calling the OpenACC function that initializes the library and | |
22 | allocate the host hardware resources, you need to acquire the device number | |
23 | that was allocated during the call to ``cublasCreate()``. The invoking of the | |
24 | runtime library function ``cudaGetDevice()`` accomplishes this. Once | |
25 | acquired, the device number is passed along with the device type as | |
26 | parameters to the OpenACC library function ``acc_set_device_num()``. | |
27 | ||
28 | Once the call to ``acc_set_device_num()`` has completed, the OpenACC | |
29 | library uses the context that was created during the call to | |
30 | ``cublasCreate()``. In other words, both libraries will be sharing the | |
31 | same context. | |
32 | ||
33 | .. code-block:: c++ | |
34 | ||
35 | /* Create the handle */ | |
36 | s = cublasCreate(&h); | |
37 | if (s != CUBLAS_STATUS_SUCCESS) | |
38 | { | |
39 | fprintf(stderr, "cublasCreate failed %d\n", s); | |
40 | exit(EXIT_FAILURE); | |
41 | } | |
42 | ||
43 | /* Get the device number */ | |
44 | e = cudaGetDevice(&dev); | |
45 | if (e != cudaSuccess) | |
46 | { | |
47 | fprintf(stderr, "cudaGetDevice failed %d\n", e); | |
48 | exit(EXIT_FAILURE); | |
49 | } | |
50 | ||
51 | /* Initialize OpenACC library and use device 'dev' */ | |
3ed1b4ce | 52 | acc_set_device_num(dev, acc_device_nvidia); |