-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathCudaSurface.cs
316 lines (285 loc) · 11.3 KB
/
CudaSurface.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2023, Michael Kunz and Artic Imaging SARL. All rights reserved.
// http://kunzmi.github.io/managedCuda
//
// This file is part of ManagedCuda.
//
// Commercial License Usage
// Licensees holding valid commercial ManagedCuda licenses may use this
// file in accordance with the commercial license agreement provided with
// the Software or, alternatively, in accordance with the terms contained
// in a written agreement between you and Artic Imaging SARL. For further
// information contact us at [email protected].
//
// GNU General Public License Usage
// Alternatively, this file may be used under the terms of the GNU General
// Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ManagedCuda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using ManagedCuda.BasicTypes;
using System.Diagnostics;
namespace ManagedCuda
{
/// <summary>
/// CudaSurface3D
/// </summary>
[Obsolete("Texture and surface references are deprecated since CUDA 11")]
public class CudaSurface : IDisposable
{
private CUsurfref _surfref;
private CUSurfRefSetFlags _flags;
private CUArrayFormat _format;
private SizeT _height;
private SizeT _width;
private SizeT _depth;
private uint _channelSize;
private SizeT _dataSize;
private int _numChannels;
private string _name;
private CUmodule _module;
private CUfunction _cufunction;
private CudaArray3D _array;
private CUResult res;
private bool disposed;
private bool _isOwner;
#region Construtors
/// <summary>
/// Creates a new surface from array memory. Allocates new array.
/// </summary>
/// <param name="kernel"></param>
/// <param name="surfName"></param>
/// <param name="flags"></param>
/// <param name="format"></param>
/// <param name="width">In elements</param>
/// <param name="height">In elements</param>
/// <param name="depth">In elements</param>
/// <param name="numChannels"></param>
/// <param name="arrayFlags"></param>
public CudaSurface(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CUArrayFormat format, SizeT width, SizeT height, SizeT depth, CudaArray3DNumChannels numChannels, CUDAArray3DFlags arrayFlags)
{
_surfref = new CUsurfref();
res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref _surfref, kernel.CUModule, surfName);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName));
if (res != CUResult.Success) throw new CudaException(res);
_flags = flags;
_format = format;
_height = height;
_width = width;
_depth = depth;
_numChannels = (int)numChannels;
_name = surfName;
_module = kernel.CUModule;
_cufunction = kernel.CUFunction;
_channelSize = CudaHelperMethods.GetChannelSize(format);
_dataSize = height * width * depth * _numChannels * _channelSize;
_array = new CudaArray3D(format, width, height, depth, numChannels, arrayFlags);
res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(_surfref, _array.CUArray, flags);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res));
if (res != CUResult.Success) throw new CudaException(res);
_isOwner = true;
}
/// <summary>
/// Creates a new surface from array memory.
/// </summary>
/// <param name="kernel"></param>
/// <param name="surfName"></param>
/// <param name="flags"></param>
/// <param name="array"></param>
public CudaSurface(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CudaArray3D array)
{
_surfref = new CUsurfref();
res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref _surfref, kernel.CUModule, surfName);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName));
if (res != CUResult.Success) throw new CudaException(res);
_flags = flags;
_format = array.Array3DDescriptor.Format;
_height = array.Height;
_width = array.Width;
_depth = array.Depth;
_numChannels = (int)array.Array3DDescriptor.NumChannels;
_name = surfName;
_module = kernel.CUModule;
_cufunction = kernel.CUFunction;
_channelSize = CudaHelperMethods.GetChannelSize(array.Array3DDescriptor.Format);
_dataSize = array.Height * array.Width * array.Depth * array.Array3DDescriptor.NumChannels * _channelSize;
_array = array;
res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(_surfref, _array.CUArray, flags);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res));
if (res != CUResult.Success) throw new CudaException(res);
_isOwner = false;
}
/// <summary>
/// For dispose
/// </summary>
~CudaSurface()
{
Dispose(false);
}
#endregion
#region Dispose
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// For IDisposable
/// </summary>
/// <param name="fDisposing"></param>
protected virtual void Dispose(bool fDisposing)
{
if (fDisposing && !disposed)
{
if (_isOwner)
{
//Dispose the internal array only if it was created internally
_array.Dispose();
}
disposed = true;
// the _surfref reference is not destroyed explicitly, as it is done automatically when module is unloaded
}
if (!fDisposing && !disposed)
Debug.WriteLine(String.Format("ManagedCUDA not-disposed warning: {0}", this.GetType()));
}
#endregion
#region Properties
/// <summary>
/// SurfaceReference
/// </summary>
public CUsurfref SurfaceReference
{
get { return _surfref; }
}
/// <summary>
/// Flags
/// </summary>
public CUSurfRefSetFlags Flags
{
get { return _flags; }
}
/// <summary>
/// Format
/// </summary>
public CUArrayFormat Format
{
get { return _format; }
}
/// <summary>
/// Height
/// </summary>
public SizeT Height
{
get { return _height; }
}
/// <summary>
/// Width
/// </summary>
public SizeT Width
{
get { return _width; }
}
/// <summary>
/// ChannelSize
/// </summary>
public uint ChannelSize
{
get { return _channelSize; }
}
/// <summary>
/// TotalSizeInBytes
/// </summary>
public SizeT TotalSizeInBytes
{
get { return _dataSize; }
}
/// <summary>
/// NumChannels
/// </summary>
public int NumChannels
{
get { return _numChannels; }
}
/// <summary>
/// Name
/// </summary>
public string Name
{
get { return _name; }
}
/// <summary>
/// Module
/// </summary>
public CUmodule Module
{
get { return _module; }
}
/// <summary>
/// CUFuntion
/// </summary>
public CUfunction CUFuntion
{
get { return _cufunction; }
}
/// <summary>
/// Array
/// </summary>
public CudaArray3D Array
{
get { return _array; }
}
#endregion
#region Static
/// <summary>
/// Create a new CudaArray3D and bind it to a surface reference.
/// </summary>
/// <param name="kernel"></param>
/// <param name="surfName"></param>
/// <param name="flags"></param>
/// <param name="format"></param>
/// <param name="width">In elements</param>
/// <param name="height">In elements</param>
/// <param name="depth">In elements</param>
/// <param name="numChannels"></param>
/// <param name="arrayFlags"></param>
public static CudaArray3D BindArray(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CUArrayFormat format, SizeT width, SizeT height, SizeT depth, CudaArray3DNumChannels numChannels, CUDAArray3DFlags arrayFlags)
{
CUsurfref surfref = new CUsurfref();
CUResult res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref surfref, kernel.CUModule, surfName);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName));
if (res != CUResult.Success) throw new CudaException(res);
CudaArray3D array = new CudaArray3D(format, width, height, depth, numChannels, arrayFlags);
res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(surfref, array.CUArray, flags);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res));
if (res != CUResult.Success) throw new CudaException(res);
return array;
}
/// <summary>
/// Bind a CudaArray3D to a surface reference.
/// </summary>
/// <param name="kernel"></param>
/// <param name="surfName"></param>
/// <param name="flags"></param>
/// <param name="array"></param>
public static void BindArray(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CudaArray3D array)
{
CUsurfref surfref = new CUsurfref();
CUResult res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref surfref, kernel.CUModule, surfName);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName));
if (res != CUResult.Success) throw new CudaException(res);
res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(surfref, array.CUArray, flags);
Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res));
if (res != CUResult.Success) throw new CudaException(res);
}
#endregion
}
}