NetSci
Loading...
Searching...
No Matches
cuarray.h
1//
2// Created by andy on 3/23/23.
3//
4
5#ifndef MUTUAL_INFORMATION_SHARED_MEMORY_CUARRAY_H
6#define MUTUAL_INFORMATION_SHARED_MEMORY_CUARRAY_H
7
8#include <cstddef>
9#include <string>
10
11typedef int CuArrayError;
12
13/*!
14 * @class CuArray
15 * @param T Data type of the array elements.
16 * @brief Manages CUDA-supported arrays, offering initialization,
17 * memory management, and data manipulation. Implemented as a template
18 * class in C++, with Python and Tcl wrapper interfaces. In Python and
19 * Tcl, use as <ElementType>CuArray (e.g., FloatCuArray, IntCuArray), as
20 * they don't support templates. Supports float and int types in Python
21 * and Tcl, and all numeric types in C++.
22 */
23template<typename T>
24class CuArray {
25public:
26 /*!
27 * @function{CuArray} @type{constructor}
28 * @brief Constructs an empty CuArray object.
29 *
30 * @CppExample{CuArray_CuArray.cpp}
31 *
32 * @PythonExample{CuArray___init__.py}
33 */
35
36/*!
37 * @function{init} @type{CuArrayError}
38 * @brief Initializes CuArray with specified dimensions and allocates
39 * memory on host and device.
40 *
41 * @param m Number of rows.
42 * @param n Number of columns.
43 * @return CuArrayError indicating operation success or failure.
44 *
45 * @CppExample{CuArray_init1.cpp}
46 * @PythonExample{CuArray_init.py}
47 */
48
49 CuArrayError init(
50 int m,
51 int n
52 );
53
54 /*!
55 * @function{init} @type{CuArrayError}
56 * @brief Initializes CuArray with specified host data and dimensions, performing a shallow copy.
57 * Allocates memory on both the host and the device. The data is shallow copied, so the ownership remains unchanged.
58 *
59 * @param host Pointer to input host data.
60 * @param m Number of rows.
61 * @param n Number of columns.
62 * @return CuArrayError indicating operation success or failure.
63 *
64 * @CppExample{CuArray_init2.cpp}
65 */
66
67
68 CuArrayError init(
69 T *host,
70 int m,
71 int n
72 );
73
74 /*!
75 * @function{fromCuArrayShallowCopy} @type{CuArrayError}
76 * @brief Performs a shallow copy of data from another CuArray within a specified row range.
77 * Copies the host data from the given CuArray, within the inclusive range
78 * specified by 'start' and 'end'. This CuArray does not own the copied data,
79 * and deallocation is handled by the source CuArray.
80 *
81 * @param cuArray Pointer to the source CuArray.
82 * @param start Index of the first row to copy.
83 * @param end Index of the last row to copy.
84 * @param m Number of rows in this CuArray.
85 * @param n Number of columns in this CuArray.
86 * @return CuArrayError indicating the operation's success or failure.
87 *
88 * @CppExample{CuArray_fromCuArrayShallowCopy.cpp}
89 */
90
92 CuArray<T> *cuArray,
93 int start,
94 int end,
95 int m,
96 int n
97 );
98
99 /*!
100 * @function{fromCuArrayDeepCopy} @type{CuArrayError}
101 * @brief Performs a deep copy of data from another CuArray within a specified row range.
102 * Copies the host data from the given CuArray, including all data within the inclusive range defined
103 * by 'start' and 'end'. Memory for the copied data is allocated in this CuArray's host memory.
104 *
105 * @param cuArray Pointer to the source CuArray.
106 * @param start Index of the first row to copy.
107 * @param end Index of the last row to copy.
108 * @param m Number of rows in this CuArray.
109 * @param n Number of columns in this CuArray.
110 * @return CuArrayError indicating the operation's success or failure.
111 *
112 * @CppExample{CuArray_fromCuArrayDeepCopy.cpp}
113 * @PythonExample{CuArray_fromCuArray.py}
114 */
115
117 CuArray<T> *cuArray,
118 int start,
119 int end,
120 int m,
121 int n
122 );
123
124 /*!
125 * @brief Destructor for CuArray.
126 * Deallocates memory on both the host and the device.
127 */
128
130
131 /*!
132 * @function{n} @type{int}
133 * @brief Returns the number of columns in the CuArray.
134 *
135 * @return Number of columns.
136 *
137 * @CppExample{CuArray_n.cpp}
138
139 * @PythonExample{CuArray_n.py}
140 */
141 int n() const;
142
143/*!
144 * @function{m} @type{int}
145 * @brief Returns the number of rows in the CuArray.
146 *
147 * @return Number of rows.
148 *
149 * @CppExample{CuArray_m.cpp}
150
151 * @PythonExample{CuArray_m.py}
152 */
153 int m() const;
154
155/*!
156 * @function{size} @type{int}
157 * @brief Returns the total number of elements in the CuArray.
158 *
159 * @return Total number of elements (rows multiplied by columns).
160 *
161 * @CppExample{CuArray_size.cpp}
162
163 * @PythonExample{CuArray_size.py}
164 */
165 int size() const;
166
167
168 /*!
169 * @function{bytes} @type{size_t}
170 * @brief Returns the total size in bytes of the CuArray data.
171 *
172 * Includes both the host and device memory.
173 *
174 * @return Size in bytes.
175 *
176 * @CppExample{CuArray_bytes.cpp}
177
178 * @PythonExample{CuArray_bytes.py}
179 */
180 size_t bytes() const;
181
182/*!
183 * @function{host} @type{T *&}
184 * @brief Returns a reference to the host data.
185 *
186 * @return Reference to the host data.
187 *
188 * @CppExample{CuArray_host.cpp}
189 */
190 T *&host();
191
192/*!
193 * @function{device} @type{T}
194 * @brief Returns a reference to the device data.
195 *
196 * @return Reference to the device data.
197 *
198 * @CppExample{CuArray_device.cpp}
199 */
200 T *&device();
201
202
203 /*!
204 * @function{allocateHost} @type{CuArrayError}
205 * @brief Allocates memory for the host data.
206 *
207 * @return CuArrayError indicating success or failure of the operation.
208 *
209 * @CppExample{CuArray_allocateHost.cpp}
210 */
211 CuArrayError allocateHost();
212
213/*!
214 * @function{allocateDevice} @type{CuArrayError}
215 * @brief Allocates memory for the device data.
216 *
217 * @return CuArrayError indicating success or failure of the operation.
218 *
219 * @CppExample{CuArray_allocateDevice.cpp}
220 */
221 CuArrayError allocateDevice();
222
223/*!
224 * @function{allocatedHost} @type{CuArrayError}
225 * @brief Checks if memory is allocated for the host data.
226 *
227 * @return CuArrayError indicating success or failure of the operation.
228 *
229 * @CppExample{CuArray_allocatedHost.cpp}
230 */
231 CuArrayError allocatedHost() const;
232
233/*!
234 * @function{allocatedDevice} @type{CuArrayError}
235 * @brief Checks if memory is allocated for the device data.
236 *
237 * @return CuArrayError indicating success or failure of the operation.
238 *
239 * @CppExample{CuArray_allocatedDevice.cpp}
240 */
241 CuArrayError allocatedDevice() const;
242
243/*!
244 * @function{toDevice} @type{CuArrayError}
245 * @brief Copies data from the host to the device.
246 *
247 * @return CuArrayError indicating success or failure of the operation.
248 *
249 * @CppExample{CuArray_toDevice.cpp}
250 */
251 CuArrayError toDevice();
252
253
254 /*!
255 * @function{toHost} @type{CuArrayError}
256 * @brief Copies data from the device to the host.
257 *
258 * @return CuArrayError indicating success or failure of the operation.
259 *
260 * @CppExample{CuArray_toHost.cpp}
261 */
262 CuArrayError toHost();
263
264/*!
265 * @function{deallocateHost} @type{CuArrayError}
266 * @brief Deallocates memory for the host data.
267 *
268 * @return CuArrayError indicating success or failure of the operation.
269 *
270 * @CppExample{CuArray_deallocateHost.cpp}
271 */
272 CuArrayError deallocateHost();
273
274/*!
275 * @function{deallocateDevice} @type{CuArrayError}
276 * @brief Deallocates memory for the device data.
277 *
278 * @return CuArrayError indicating success or failure of the operation.
279 *
280 * @CppExample{CuArray_deallocateDevice.cpp}
281 */
282 CuArrayError deallocateDevice();
283
284/*!
285 * @function{fromNumpy} @type{CuArrayError}
286 * @brief Copies data from a NumPy array to the CuArray.
287 *
288 * @param NUMPY_ARRAY Pointer to the input NumPy array.
289 * @param NUMPY_ARRAY_DIM1 Dimension 1 of the NumPy array.
290 * @param NUMPY_ARRAY_DIM2 Dimension 2 of the NumPy array.
291 * @return CuArrayError indicating success or failure of the operation.
292 *
293 * @CppExample{CuArray_fromNumpy2.cpp}
294 *
295 * @PythonExample{CuArray_fromNumpy2D.py}
296 */
297 CuArrayError fromNumpy(
298 T *NUMPY_ARRAY,
299 int NUMPY_ARRAY_DIM1,
300 int NUMPY_ARRAY_DIM2
301 );
302
303/*!
304 * @function{fromNumpy} @type{CuArrayError}
305 * @brief Copies data from a NumPy array to the CuArray.
306 *
307 * @param NUMPY_ARRAY Pointer to input NumPy array.
308 * @param NUMPY_ARRAY_DIM1 Dimension 1 of the NumPy array.
309 * @return CuArrayError indicating success or failure of the operation.
310 *
311 * @CppExample{CuArray_fromNumpy1.cpp}
312 *
313 * @PythonExample{CuArray_fromNumpy1D.py}
314 */
315 CuArrayError fromNumpy(
316 T *NUMPY_ARRAY,
317 int NUMPY_ARRAY_DIM1
318 );
319
320/*!
321 * @function{toNumpy} @type{void}
322 * @brief Copies data from the CuArray to a NumPy array.
323 *
324 * @param NUMPY_ARRAY Pointer to output NumPy array.
325 * @param NUMPY_ARRAY_DIM1 Dimension 1 of the NumPy array.
326 * @param NUMPY_ARRAY_DIM2 Dimension 2 of the NumPy array.
327 *
328 * @CppExample{CuArray_toNumpy2.cpp}
329 *
330 * @PythonExample{CuArray_toNumpy2D.py}
331 */
333 T **NUMPY_ARRAY,
334 int **NUMPY_ARRAY_DIM1,
335 int **NUMPY_ARRAY_DIM2
336 );
337
338/*!
339 * @function{toNumpy} @type{void}
340 * @brief Copies data from the CuArray to a NumPy array.
341 *
342 * @param NUMPY_ARRAY Pointer to output NumPy array.
343 * @param NUMPY_ARRAY_DIM1 Dimension 1 of the NumPy array.
344 *
345 * @CppExample{CuArray_toNumpy1.cpp}
346 *
347 * @PythonExample{CuArray_toNumpy1D.py}
348 */
350 T **NUMPY_ARRAY,
351 int **NUMPY_ARRAY_DIM1
352 );
353
354/*!
355 * @function{get} @type{T}
356 * @brief Returns the value at a specified position in the CuArray.
357 *
358 * @param i Row index.
359 * @param j Column index.
360 * @return Value at the specified position.
361 *
362 * @CppExample{CuArray_get.cpp}
363 * @PythonExample{CuArray_get.py}
364 */
366 int i,
367 int j
368 ) const;
369
370
371 /*!
372 * @function{set} @type{CuArrayError}
373 * @brief Sets a value at a specified position in the CuArray.
374 *
375 * @param value Value to set.
376 * @param i Row index.
377 * @param j Column index.
378 * @return CuArrayError indicating success or failure of the operation.
379 *
380 * @CppExample{CuArray_set.cpp}
381 * @PythonExample{CuArray_set.py}
382 */
383 CuArrayError set(
384 T value,
385 int i,
386 int j
387 );
388
389/*!
390 * @function{load} @type{CuArrayError}
391 * @brief Loads CuArray data from a specified file.
392 *
393 * @param fname File name to load from.
394 * @return CuArrayError indicating success or failure of the operation.
395 *
396 * @CppExample{CuArray_load.cpp}
397 * @PythonExample{CuArray_load.py}
398 */
399 CuArrayError load(const std::string &fname);
400
401/*!
402 * @function{save} @type{void}
403 * @brief Saves CuArray data to a specified file.
404 *
405 * @param fname File name to save to.
406 *
407 * @CppExample{CuArray_save.cpp}
408 * @PythonExample{CuArray_save.py}
409 */
410 void save(const std::string &fname);
411
412/*!
413 * @function{sort} @type{CuArray<T>}
414 * @brief Sorts CuArray based on the values in a specified row.
415 *
416 * @param i Index of the row to sort by.
417 * @return Pointer to a new CuArray with sorted data.
418 *
419 * @CppExample{CuArray_sort.cpp}
420 * @PythonExample{CuArray_sort.py}
421 */
423
424/*!
425 * @function{operator[]} @type{T &}
426 * @brief Returns a reference to the element at a specified index in the CuArray.
427 *
428 * @param i Index of the element.
429 * @return Reference to the element at the specified index.
430 *
431 * @CppExample{CuArray_subscriptOperator.cpp}
432 * @PythonExample{CuArray___getitem__.py}
433 */
434 T &operator[](int i) const;
435
436/*!
437 * @function{owner} @type{int}
438 * @brief Returns the owner status of the CuArray.
439 * Indicates whether the CuArray is responsible for memory deallocation.
440 *
441 * @return Owner status of the CuArray.
442 *
443 * @CppExample{CuArray_owner.cpp}
444 */
445 int owner() const;
446
447/*!
448 * @function{argsort} @type{CuArray}
449 * @brief Performs an argsort on a specified row of the CuArray.
450 * Returns a new CuArray containing sorted indices.
451 *
452 * @param i Column index to argsort.
453 * @return Pointer to a new CuArray with sorted indices.
454 *
455 * @CppExample{CuArray_argsort.cpp}
456 * @PythonExample{CuArray_argsort.py}
457 */
459
460private:
461 T *host_;
462 T *device_;
463 int n_{};
464 int m_{};
465 int size_{};
466 size_t bytes_{};
467 int allocatedDevice_{};
468 int allocatedHost_{};
469 int owner_{};
470};
471
472template<typename T>
474public:
476 CuArray<T> *cuArray,
477 int i
478 );
479
480 T &operator[](int i) const;
481
482 int n() const;
483
484 T *data() const;
485
486private:
487 T *data_;
488 int n_{};
489};
490
491#endif // MUTUAL_INFORMATION_SHARED_MEMORY_CUARRAY_H
Definition cuarray.h:473
Manages CUDA-supported arrays, offering initialization, memory management, and data manipulation....
Definition cuarray.h:24
int m() const
Returns the number of rows in the CuArray.
void toNumpy(T **NUMPY_ARRAY, int **NUMPY_ARRAY_DIM1, int **NUMPY_ARRAY_DIM2)
Copies data from the CuArray to a NumPy array.
CuArrayError allocatedHost() const
Checks if memory is allocated for the host data.
CuArrayError toHost()
Copies data from the device to the host.
T get(int i, int j) const
Returns the value at a specified position in the CuArray.
CuArrayError init(T *host, int m, int n)
Initializes CuArray with specified host data and dimensions, performing a shallow copy....
CuArrayError allocateHost()
Allocates memory for the host data.
CuArrayError toDevice()
Copies data from the host to the device.
CuArrayError allocatedDevice() const
Checks if memory is allocated for the device data.
CuArrayError fromNumpy(T *NUMPY_ARRAY, int NUMPY_ARRAY_DIM1, int NUMPY_ARRAY_DIM2)
Copies data from a NumPy array to the CuArray.
int n() const
Returns the number of columns in the CuArray.
CuArrayError deallocateDevice()
Deallocates memory for the device data.
CuArrayError set(T value, int i, int j)
Sets a value at a specified position in the CuArray.
void save(const std::string &fname)
Saves CuArray data to a specified file.
CuArrayError allocateDevice()
Allocates memory for the device data.
T *& device()
Returns a reference to the device data.
CuArrayError fromCuArrayShallowCopy(CuArray< T > *cuArray, int start, int end, int m, int n)
Performs a shallow copy of data from another CuArray within a specified row range....
T *& host()
Returns a reference to the host data.
int owner() const
Returns the owner status of the CuArray. Indicates whether the CuArray is responsible for memory deal...
CuArrayError init(int m, int n)
Initializes CuArray with specified dimensions and allocates memory on host and device.
CuArrayError fromCuArrayDeepCopy(CuArray< T > *cuArray, int start, int end, int m, int n)
Performs a deep copy of data from another CuArray within a specified row range. Copies the host data ...
void toNumpy(T **NUMPY_ARRAY, int **NUMPY_ARRAY_DIM1)
Copies data from the CuArray to a NumPy array.
CuArrayError fromNumpy(T *NUMPY_ARRAY, int NUMPY_ARRAY_DIM1)
Copies data from a NumPy array to the CuArray.
CuArrayError load(const std::string &fname)
Loads CuArray data from a specified file.
~CuArray()
Destructor for CuArray. Deallocates memory on both the host and the device.
CuArray< T > * sort(int i)
Sorts CuArray based on the values in a specified row.
CuArrayError deallocateHost()
Deallocates memory for the host data.
int size() const
Returns the total number of elements in the CuArray.
CuArray< int > * argsort(int i)
Performs an argsort on a specified row of the CuArray. Returns a new CuArray containing sorted indice...
T & operator[](int i) const
Returns a reference to the element at a specified index in the CuArray.
CuArray()
Constructs an empty CuArray object.
size_t bytes() const
Returns the total size in bytes of the CuArray data.