CublasHandleManager.h.prehip 1.84 KB
Newer Older
wangkx1's avatar
init  
wangkx1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*! \file CublasHandleManager.h
    \brief Contains declaration and definition of a single class used to manage a cuBlas handle.

    \author Jesper Andersson
    \version 1.0b, Sep., 2012.
*/
// Declarations of classes that implements useful
// concepts for the eddy current project.
//
// CublasHandleManager.h
//
// Jesper Andersson, FMRIB Image Analysis Group
//
// Copyright (C) 2020 University of Oxford
//

// Because of a bug in cuda_fp16.hpp, that gets included by cublas_v2.h, it has to
// be included before any include files that set up anything related to the std-lib.
// If not, there will be an ambiguity in cuda_fp16.hpp about wether to use the
// old-style C isinf or the new (since C++11) std::isinf.

#if !defined(CUBLAS_V2_H_)
#error cublas_v2.h must be included at the very top of any file including CublasHandleManager.h
#endif

#include <stdio.h>
#pragma push
#pragma diag_suppress = code_is_unreachable // Supress warnings from armawrap
#pragma diag_suppress = expr_has_no_effect  // Supress warnings from boost
#include "armawrap/newmat.h"
#include "miscmaths/miscmaths.h"
#include "newimage/newimageall.h"
#pragma pop
#include "EddyHelperClasses.h"
#include <cuda.h>
#include <cuda_runtime.h>


class CublasHandleManager
{
public:
  CublasHandleManager(const CublasHandleManager&) = delete;
  CublasHandleManager& operator=(const CublasHandleManager&) = delete;
  static cublasHandle_t& GetHandle();
private:
  CublasHandleManager() {
    cublasStatus_t status;
    status = cublasCreate(&_handle);
    if (status != CUBLAS_STATUS_SUCCESS) throw EDDY::EddyException("EDDY::CublasHandleManager::CublasHandleManager: cuBLAS initialization failed");
  }
  ~CublasHandleManager() { cublasDestroy(_handle); }

  cublasHandle_t _handle;
};

cublasHandle_t& CublasHandleManager::GetHandle()
{
  static CublasHandleManager instance;
  return(instance._handle);
}