MAGMA  1.2.0
MatrixAlgebraonGPUandMulticoreArchitectures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
init.cpp
Go to the documentation of this file.
1 /*
2  -- MAGMA (version 1.2.0) --
3  Univ. of Tennessee, Knoxville
4  Univ. of California, Berkeley
5  Univ. of Colorado, Denver
6  May 2012
7 */
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <pthread.h>
12 
13 #include <cuda.h>
14 #include <cuda_runtime_api.h>
15 #include <cublas.h>
16 
17 #include <quark.h>
18 
19 #include "magma.h"
20 
21 typedef struct {
22  int tid;
23  void *params;
24 } t_params;
25 
26 extern "C" magma_context *
27 magma_init( void *params, void* (*func)(void *a), magma_int_t nthread, magma_int_t ncpu, magma_int_t ngpu,
28  magma_int_t argc, char **argv)
29 {
30 /* -- MAGMA (version 1.2.0) --
31  Univ. of Tennessee, Knoxville
32  Univ. of California, Berkeley
33  Univ. of Colorado, Denver
34  May 2012
35 
36  Purpose
37  =======
38  This function initializes the hardware context to be used for
39  subsequent calls to routines in the MAGMA library.
40 
41  Arguments
42  =========
43  NCPU (input) INTEGER
44  Number of CPU cores to be used in the computations.
45 
46  NGPU (input) INTEGER
47  Number of GPU cores to be used in the computations.
48  ===================================================================== */
49 
50  t_params **tp = (t_params**)malloc(sizeof(t_params*)*nthread);
51 
52  pthread_t *thread;
53 
54  magma_int_t i;
56  context = (magma_context *)malloc(sizeof(magma_context));
57 
58  if (nthread > 0) {
59  thread = (pthread_t*)malloc(sizeof(pthread_t)*nthread);
60 
61  for (i = 0; i < nthread; i++){
62  tp[i] = (t_params*)malloc(sizeof(t_params));
63  tp[i]->params = params;
64  tp[i]->tid = i;
65  pthread_create(&thread[i], NULL, func, (void *)tp[i]);
66  }
67  }
68 
69 
70  if (ncpu <= 1)
71  ncpu = 1;
72 
73  if (ngpu <= 0)
74  ngpu = 0;
75 
76  context->num_cores = ncpu;
77  context->num_gpus = ngpu;
78 
79  if (ncpu > 1)
80  {
81  /* Initialize the QUARK scheduler */
82  context->quark = QUARK_New(ncpu);
83  }
84 
85  if (ngpu > 1)
86  {
87  printf("The requested number of GPUs is not yet supported.\n\n");
88  printf("The number of GPUs set to one.\n\n");
89  context->num_gpus = 1;
90  }
91 
92  if (ngpu == 1)
93  {
94  CUdevice dev;
95  context->gpu_context = (CUcontext *)malloc(ngpu * sizeof(CUcontext));
96 
97  /* For now we use by default device 0, always */
98  if( CUDA_SUCCESS != cuInit( 0 ) ) {
99  fprintf(stderr, "CUDA: Not initialized\n" );
100  exit(-1);
101  }
102  if( CUDA_SUCCESS != cuDeviceGet( &dev, 0 ) ) {
103  fprintf(stderr, "CUDA: Cannot get the device\n");
104  exit(-1);
105  }
106  if( CUDA_SUCCESS != cuCtxCreate( &context->gpu_context[0], 0, dev ) ) {
107  fprintf(stderr, "CUDA: Cannot create the context\n");
108  exit(-1);
109  }
110  if( CUDA_SUCCESS != cublasInit( ) ) {
111  fprintf(stderr, "CUBLAS: Not initialized\n");
112  exit(-1);
113  }
114  printout_devices( );
115  }
116 
117  context->nb = -1;
118  for(i = 1; i<argc; i++)
119  if (strcmp("-b", argv[i])==0)
120  context->nb = atoi(argv[++i]);
121 
122  return context;
123 }
124 
125 
126 extern "C" void
128 {
129 /* -- MAGMA (version 1.2.0) --
130  Univ. of Tennessee, Knoxville
131  Univ. of California, Berkeley
132  Univ. of Colorado, Denver
133  May 2012
134 
135  Purpose
136  =======
137  This function finalizes the MAGMA hardware context.
138 
139  Arguments
140  =========
141  CNTXT (input) MAGMA_CONTEXT
142  Pointer to the MAGMA hardware context to be closed
143  ===================================================================== */
144 
145  if (cntxt->num_cores > 1)
146  /* Shut down the QUARK scheduler */
147  QUARK_Delete(cntxt->quark);
148 
149  if (cntxt->num_gpus == 1)
150  {
151  /* Shutdown CUDA and CUBLAS*/
152  cuCtxDetach( cntxt->gpu_context[0] );
153  cublasShutdown();
154 
155  free(cntxt->gpu_context);
156  }
157 
158  free(cntxt);
159 }