-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUDAFlatIndex.h
151 lines (133 loc) · 2.81 KB
/
CUDAFlatIndex.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
#ifndef __CUDAFlatIndex_h
#define __CUDAFlatIndex_h
#include "CartesianExtent.h"
#include "CUDAMemoryManager.hxx"
#include <cuda.h>
#include <cuda_runtime.h>
/// CUDAFlatIndex - A class to convert i,j,k tuples into flat indices
/**
CUDAFlatIndex - A class to convert i,j,k tuples into flat indices
The following formula is applied:
<pre>
mode -> k*(A) + j*(B) +i*(C)
--------------------------------------
3d -> k*(ninj) + j*(ni) + i
xy -> j*(ni) + i
xz -> k*(ni) + i
yz -> k*(nj) + j
--------------------------------------
</pre>
*/
class CUDAFlatIndex
{
public:
///
__host__
CUDAFlatIndex()
:
A(0),
B(0),
C(0),
DeviceObject(0)
{}
///
__host__
CUDAFlatIndex(const CartesianExtent &patch, int mode)
:
DeviceObject(0)
{
int size[3];
patch.Size(size);
this->Initialize(size[0],size[1],size[2],mode);
}
///
__host__
CUDAFlatIndex(
unsigned long ni,
unsigned long nj,
unsigned long nk,
int mode)
:
DeviceObject(0)
{
this->Initialize(ni,nj,nk,mode);
}
///
__host__
void Initialize(
unsigned long ni,
unsigned long nj,
unsigned long nk,
int mode)
{
switch(mode)
{
case CartesianExtent::DIM_MODE_2D_XZ:
this->A = ni;
this->B = 0;
this->C = 1;
break;
case CartesianExtent::DIM_MODE_2D_YZ:
this->A = nj;
this->B = 1;
this->C = 0;
break;
case CartesianExtent::DIM_MODE_2D_XY:
this->A = 0;
this->B = ni;
this->C = 1;
break;
case CartesianExtent::DIM_MODE_3D:
this->A = ni*nj;
this->B = ni;
this->C = 1;
break;
}
delete this->DeviceObject;
this->DeviceObject
= CUDAMemoryManager<CUDAFlatIndex>::New(this);
this->DeviceObject->Push();
}
///
__host__
~CUDAFlatIndex()
{
delete this->DeviceObject;
}
///
__host__
CUDAFlatIndex *GetDevicePointer()
{
return this->DeviceObject->GetDevicePointer();
}
///
__device__
unsigned long operator()(
unsigned long i,
unsigned long j,
unsigned long k)
{
return k*this->A + j*this->B + i*this->C;
}
///
__device__
void Print(const char *name)
{
printf("[%i %i] %s=(A=%lu B=%lu C=%lu)\n",
blockIdx.x,threadIdx.x,name,this->A,this->B,this->C);
}
private:
unsigned long A;
unsigned long B;
unsigned long C;
//
CUDAMemoryManager<CUDAFlatIndex> *DeviceObject;
};
#endif