| 1 |
#ifndef __PLINK2_MATRIX_CUDA_H__ |
| 2 |
#define __PLINK2_MATRIX_CUDA_H__ |
| 3 |
|
| 4 |
// This file is part of PLINK 2.00, copyright (C) 2005-2020 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This program is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU General Public License as published by the Free |
| 9 |
// Software Foundation, either version 3 of the License, or (at your option) |
| 10 |
// any later version. |
| 11 |
// |
| 12 |
// This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
// more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// CUDA-specific portion of plink2_matrix, to be compiled with nvcc instead of |
| 21 |
// gcc/clang. |
| 22 |
|
| 23 |
#ifdef __cplusplus |
| 24 |
extern "C" { |
| 25 |
#endif |
| 26 |
|
| 27 |
// Avoid importing cuda_runtime.h and cublas_v2.h here, since it uses raw |
| 28 |
// casts, etc. |
| 29 |
|
| 30 |
int CudaGetDeviceCount(); |
| 31 |
|
| 32 |
int CudaSetDevice(int device_idx); |
| 33 |
|
| 34 |
typedef struct CublasFmultiplierStruct { |
| 35 |
void* handle; // known to be a pointer |
| 36 |
float* dev_inmatrix1; |
| 37 |
float* dev_inmatrix2; |
| 38 |
float* dev_outmatrix; |
| 39 |
int row1_ct; |
| 40 |
int col2_ct; |
| 41 |
int common_ct; |
| 42 |
unsigned int in_borrowed; |
| 43 |
int device_idx; |
| 44 |
} CublasFmultiplier; |
| 45 |
|
| 46 |
void CublasFmultiplierPreinit(CublasFmultiplier* cfmp); |
| 47 |
|
| 48 |
// These functions allow multiple CublasFmultipliers to share an input matrix |
| 49 |
// loaded in device memory. |
| 50 |
void CublasFmultiplierBorrowColMajor1(CublasFmultiplier* srcp, CublasFmultiplier* dstp); |
| 51 |
|
| 52 |
void CublasFmultiplierBorrowColMajor2(CublasFmultiplier* srcp, CublasFmultiplier* dstp); |
| 53 |
|
| 54 |
inline void CublasFmultiplierBorrowRowMajor1(CublasFmultiplier* srcp, CublasFmultiplier* dstp) { |
| 55 |
CublasFmultiplierBorrowColMajor2(srcp, dstp); |
| 56 |
} |
| 57 |
|
| 58 |
inline void CublasFmultiplierBorrowRowMajor2(CublasFmultiplier* srcp, CublasFmultiplier* dstp) { |
| 59 |
CublasFmultiplierBorrowColMajor1(srcp, dstp); |
| 60 |
} |
| 61 |
|
| 62 |
// Initializes device handle and allocates device memory. (Borrows should |
| 63 |
// happen first.) |
| 64 |
// Returns 0 on success, 1 on failure. |
| 65 |
int CublasFmultiplierColMajorInit(int row1_ct, int col2_ct, int common_ct, CublasFmultiplier* cfmp); |
| 66 |
|
| 67 |
inline int CublasFmultiplierRowMajorInit(int row1_ct, int col2_ct, int common_ct, CublasFmultiplier* cfmp) { |
| 68 |
return CublasFmultiplierColMajorInit(col2_ct, row1_ct, common_ct, cfmp); |
| 69 |
} |
| 70 |
|
| 71 |
int CublasFmultiplierPreloadColMajor1(const float* inmatrix1, CublasFmultiplier* cfmp); |
| 72 |
|
| 73 |
int CublasFmultiplierPreloadColMajor2(const float* inmatrix2, CublasFmultiplier* cfmp); |
| 74 |
|
| 75 |
inline int CublasFmultiplierPreloadRowMajor1(const float* inmatrix1, CublasFmultiplier* cfmp) { |
| 76 |
return CublasFmultiplierPreloadColMajor2(inmatrix1, cfmp); |
| 77 |
} |
| 78 |
|
| 79 |
inline int CublasFmultiplierPreloadRowMajor2(const float* inmatrix2, CublasFmultiplier* cfmp) { |
| 80 |
return CublasFmultiplierPreloadColMajor1(inmatrix2, cfmp); |
| 81 |
} |
| 82 |
|
| 83 |
// Assumes CublasFmultiplyPreloadColMajor2() was previously called. |
| 84 |
int CublasFmultiplyColMajor1(const float* inmatrix1, CublasFmultiplier* cfmp, float* outmatrix); |
| 85 |
|
| 86 |
int CublasFmultiplyColMajor2(const float* inmatrix2, CublasFmultiplier* cfmp, float* outmatrix); |
| 87 |
|
| 88 |
inline int CublasFmultiplyRowMajor1(const float* inmatrix1, CublasFmultiplier* cfmp, float* outmatrix) { |
| 89 |
return CublasFmultiplyColMajor2(inmatrix1, cfmp, outmatrix); |
| 90 |
} |
| 91 |
|
| 92 |
inline int CublasFmultiplyRowMajor2(const float* inmatrix2, CublasFmultiplier* cfmp, float* outmatrix) { |
| 93 |
return CublasFmultiplyColMajor1(inmatrix2, cfmp, outmatrix); |
| 94 |
} |
| 95 |
|
| 96 |
int CublasFmultiplierCleanup(CublasFmultiplier* cfmp); |
| 97 |
|
| 98 |
|
| 99 |
// Only for testing purposes: inefficient host-memory reallocation, etc. |
| 100 |
int CublasColMajorFmatrixMultiply(const float* inmatrix1, const float* inmatrix2, int row1_ct, int col2_ct, int common_ct, float* outmatrix); |
| 101 |
|
| 102 |
inline int CublasRowMajorFmatrixMultiply(const float* inmatrix1, const float* inmatrix2, int row1_ct, int col2_ct, int common_ct, float* outmatrix) { |
| 103 |
return CublasColMajorFmatrixMultiply(inmatrix2, inmatrix1, col2_ct, row1_ct, common_ct, outmatrix); |
| 104 |
} |
| 105 |
|
| 106 |
#ifdef __cplusplus |
| 107 |
} // extern "C" |
| 108 |
#endif |
| 109 |
|
| 110 |
#endif // __PLINK2_MATRIX_CUDA_H__ |