Skip to content

Commit 3eb4715

Browse files
authored
Ensure namespaces are organized and named properly (#596)
* Change namespaces so they make more sense and are more general * Fix namespace * Use device namespace explicitly * Remove extra comment * Change namespace of matrix * Fix typos * pull out the forward declarations from tsvd namespace
1 parent 42cf30e commit 3eb4715

File tree

7 files changed

+265
-254
lines changed

7 files changed

+265
-254
lines changed

src/gpu/data/matrix.cu

+92-90
Large diffs are not rendered by default.

src/gpu/data/matrix.cuh

+59-61
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#pragma once
2-
#include "../tsvd/utils.cuh"
2+
#include "../utils/utils.cuh"
33
#include "../device/device_context.cuh"
44
#include "cusolverDn.h"
55
#include <../../../cub/cub/cub.cuh>
66

7-
namespace tsvd
7+
namespace matrix
88
{
9-
10-
typedef float tsvd_float;
11-
typedef double tsvd_double;
9+
using namespace h2o4gpu;
1210

1311
/**
1412
* \class Matrix
@@ -275,19 +273,19 @@ namespace tsvd
275273

276274
void copy(const Matrix<T>& M)
277275
{
278-
tsvd_check(M.rows() == this->rows()&&M.columns() == this->columns(), "Cannot copy matrix. Dimensions are different.");
276+
h2o4gpu_check(M.rows() == this->rows()&&M.columns() == this->columns(), "Cannot copy matrix. Dimensions are different.");
279277
thrust::copy(M.dptr(), M.dptr() + M.size(), this->dptr());
280278
}
281279

282280

283281
void print() const
284282
{
285-
thrust::host_vector<T> h_tsvd(thrust::device_ptr<T>(_data), thrust::device_ptr<T>(_data + _n * _m));
283+
thrust::host_vector<T> h_matrix(thrust::device_ptr<T>(_data), thrust::device_ptr<T>(_data + _n * _m));
286284
for (auto i = 0; i < _m; i++)
287285
{
288286
for (auto j = 0; j < _n; j++)
289287
{
290-
printf("%1.2f ", h_tsvd[j * _m + i]);
288+
printf("%1.2f ", h_matrix[j * _m + i]);
291289
}
292290
printf("\n");
293291
}
@@ -306,43 +304,43 @@ namespace tsvd
306304
}
307305
};
308306

309-
void multiply_diag(const Matrix<tsvd_float>& A, const Matrix<tsvd_float>& B, Matrix<tsvd_float>& C, DeviceContext& context, bool left_diag);
310-
void multiply_diag(const Matrix<tsvd_double>& A, const Matrix<tsvd_double>& B, Matrix<tsvd_double>& C, DeviceContext& context, bool left_diag);
307+
void multiply_diag(const Matrix<float>& A, const Matrix<float>& B, Matrix<float>& C, device::DeviceContext& context, bool left_diag);
308+
void multiply_diag(const Matrix<double>& A, const Matrix<double>& B, Matrix<double>& C, device::DeviceContext& context, bool left_diag);
311309

312310
/**
313-
* \fn void multiply(const Matrix<tsvd_float>& A, const Matrix<tsvd_float>& B, Matrix<tsvd_float>& C, DeviceContext& context, bool transpose_a = false, bool transpose_b = false, tsvd_float alpha=1.0f);
311+
* \fn void multiply(const Matrix<float>& A, const Matrix<float>& B, Matrix<float>& C, device::DeviceContext& context, bool transpose_a = false, bool transpose_b = false, float alpha=1.0f);
314312
*
315313
* \brief Matrix multiplication. ABa = C. A or B may be transposed. a is a scalar.
316314
*
317315
* \param A The Matrix&lt;float&gt; to process.
318-
* \param B The Matrix&lt;tsvd_float&gt; to process.
316+
* \param B The Matrix&lt;float&gt; to process.
319317
* \param [in,out] C The Matrix&lt;float&gt; to process.
320318
* \param [in,out] context The context.
321319
* \param transpose_a (Optional) True to transpose a.
322320
* \param transpose_b (Optional) True to transpose b.
323321
* \param alpha (Optional) The alpha.
324322
*/
325323

326-
void multiply(const Matrix<tsvd_float>& A, const Matrix<tsvd_float>& B, Matrix<tsvd_float>& C, DeviceContext& context, bool transpose_a = false, bool transpose_b = false, tsvd_float alpha = 1.0f);
324+
void multiply(const Matrix<float>& A, const Matrix<float>& B, Matrix<float>& C, device::DeviceContext& context, bool transpose_a = false, bool transpose_b = false, float alpha = 1.0f);
327325

328326
/**
329-
* \fn void multiply(const Matrix<tsvd_double>& A, const Matrix<tsvd_double>& B, Matrix<tsvd_double>& C, DeviceContext& context, bool transpose_a = false, bool transpose_b = false, tsvd_double alpha=1.0f);
327+
* \fn void multiply(const Matrix<double>& A, const Matrix<double>& B, Matrix<double>& C, device::DeviceContext& context, bool transpose_a = false, bool transpose_b = false, double alpha=1.0f);
330328
*
331329
* \brief Matrix multiplication. ABa = C. A or B may be transposed. a is a scalar.
332330
*
333331
* \param A The Matrix&lt;float&gt; to process.
334-
* \param B The Matrix&lt;tsvd_double&gt; to process.
332+
* \param B The Matrix&lt;double&gt; to process.
335333
* \param [in,out] C The Matrix&lt;float&gt; to process.
336334
* \param [in,out] context The context.
337335
* \param transpose_a (Optional) True to transpose a.
338336
* \param transpose_b (Optional) True to transpose b.
339337
* \param alpha (Optional) The alpha.
340338
*/
341339

342-
void multiply(const Matrix<tsvd_double>& A, const Matrix<tsvd_double>& B, Matrix<tsvd_double>& C, DeviceContext& context, bool transpose_a = false, bool transpose_b = false, tsvd_double alpha = 1.0f);
340+
void multiply(const Matrix<double>& A, const Matrix<double>& B, Matrix<double>& C, device::DeviceContext& context, bool transpose_a = false, bool transpose_b = false, double alpha = 1.0f);
343341

344342
/**
345-
* \fn void multiply(Matrix<tsvd_float>& A, const tsvd_float a ,DeviceContext& context);
343+
* \fn void multiply(Matrix<float>& A, const float a ,device::DeviceContext& context);
346344
*
347345
* \brief Matrix scalar multiplication.
348346
*
@@ -352,97 +350,97 @@ namespace tsvd
352350
*/
353351

354352
template<typename T, typename U>
355-
void multiply(Matrix<T>& A, const U a, DeviceContext& context);
353+
void multiply(Matrix<T>& A, const U a, device::DeviceContext& context);
356354

357355
/**
358-
* \fn void matrix_sub(const Matrix<tsvd_float>& A, const Matrix<float>& B, Matrix<float>& C, DeviceContext& context)
356+
* \fn void matrix_sub(const Matrix<float>& A, const Matrix<float>& B, Matrix<float>& C, device::DeviceContext& context)
359357
*
360358
* \brief Matrix subtraction. A - B = C.
361359
*
362360
*/
363361

364362
template<typename T>
365-
void subtract(const Matrix<T>& A, const Matrix<T>& B, Matrix<T>& C, DeviceContext& context);
363+
void subtract(const Matrix<T>& A, const Matrix<T>& B, Matrix<T>& C, device::DeviceContext& context);
366364

367365
/**
368-
* \fn void add(const Matrix<tsvd_float>& A, const Matrix<tsvd_float>& B, Matrix<tsvd_float>& C, DeviceContext& context);
366+
* \fn void add(const Matrix<float>& A, const Matrix<float>& B, Matrix<float>& C, device::DeviceContext& context);
369367
*
370368
* \brief Matrix addition. A + B = C
371369
*
372-
* \param A The Matrix&lt;tsvd_float&gt; to process.
373-
* \param B The Matrix&lt;tsvd_float&gt; to process.
374-
* \param [in,out] C The Matrix&lt;tsvd_float&gt; to process.
370+
* \param A The Matrix&lt;float&gt; to process.
371+
* \param B The Matrix&lt;float&gt; to process.
372+
* \param [in,out] C The Matrix&lt;float&gt; to process.
375373
* \param [in,out] context The context.
376374
*/
377375

378376
template<typename T>
379-
void add(const Matrix<T>& A, const Matrix<T>& B, Matrix<T>& C, DeviceContext& context);
377+
void add(const Matrix<T>& A, const Matrix<T>& B, Matrix<T>& C, device::DeviceContext& context);
380378

381379
/**
382-
* \fn void transpose(const Matrix<tsvd_float >&A, Matrix<tsvd_float >&B, DeviceContext& context)
380+
* \fn void transpose(const Matrix<float >&A, Matrix<float >&B, device::DeviceContext& context)
383381
*
384382
* \brief Transposes matrix A into matrix B.
385383
*
386-
* \param A The Matrix&lt;tsvd_float&gt; to process.
387-
* \param [in,out] B The Matrix&lt;tsvd_float&gt; to process.
384+
* \param A The Matrix&lt;float&gt; to process.
385+
* \param [in,out] B The Matrix&lt;float&gt; to process.
388386
* \param [in,out] context The context.
389387
*/
390388

391-
void transpose(const Matrix<tsvd_float>& A, Matrix<tsvd_float>& B, DeviceContext& context);
389+
void transpose(const Matrix<float>& A, Matrix<float>& B, device::DeviceContext& context);
392390

393391
/**
394-
* \fn void transpose(const Matrix<tsvd_double >&A, Matrix<tsvd_double >&B, DeviceContext& context)
392+
* \fn void transpose(const Matrix<double >&A, Matrix<double >&B, device::DeviceContext& context)
395393
*
396394
* \brief Transposes matrix A into matrix B.
397395
*
398-
* \param A The Matrix&lt;tsvd_double&gt; to process.
399-
* \param [in,out] B The Matrix&lt;tsvd_double&gt; to process.
396+
* \param A The Matrix&lt;double&gt; to process.
397+
* \param [in,out] B The Matrix&lt;double&gt; to process.
400398
* \param [in,out] context The context.
401399
*/
402400

403-
void transpose(const Matrix<tsvd_double>& A, Matrix<tsvd_double>& B, DeviceContext& context);
401+
void transpose(const Matrix<double>& A, Matrix<double>& B, device::DeviceContext& context);
404402

405403
/**
406-
* \fn void normalize_columns(Matrix<tsvd_float>& M, Matrix<tsvd_float>& M_temp, Matrix<tsvd_float>& column_length, Matrix<tsvd_float>& ones, DeviceContext& context);
404+
* \fn void normalize_columns(Matrix<float>& M, Matrix<float>& M_temp, Matrix<float>& column_length, Matrix<float>& ones, device::DeviceContext& context);
407405
*
408406
* \brief Normalize matrix columns.
409407
*
410-
* \param [in,out] M The Matrix&lt;tsvd_float&gt; to process.
408+
* \param [in,out] M The Matrix&lt;float&gt; to process.
411409
* \param [in,out] M_temp Temporary storage matrix of size >= M.
412410
* \param [in,out] column_length Temporary storage matrix with one element per column.
413411
* \param [in,out] ones Matrix of ones of length M.columns().
414412
* \param [in,out] context The context.
415413
*/
416414

417-
void normalize_columns(Matrix<tsvd_float>& M, Matrix<tsvd_float>& M_temp, Matrix<tsvd_float>& column_length, const Matrix<tsvd_float>& ones, DeviceContext& context);
418-
void normalize_columns(Matrix<tsvd_double>& M, Matrix<tsvd_double>& M_temp, Matrix<tsvd_double>& column_length, const Matrix<tsvd_double>& ones, DeviceContext& context);
415+
void normalize_columns(Matrix<float>& M, Matrix<float>& M_temp, Matrix<float>& column_length, const Matrix<float>& ones, device::DeviceContext& context);
416+
void normalize_columns(Matrix<double>& M, Matrix<double>& M_temp, Matrix<double>& column_length, const Matrix<double>& ones, device::DeviceContext& context);
419417

420-
void normalize_columns(Matrix<tsvd_float>& M, DeviceContext& context);
421-
void normalize_columns(Matrix<tsvd_double>& M, DeviceContext& context);
418+
void normalize_columns(Matrix<float>& M, device::DeviceContext& context);
419+
void normalize_columns(Matrix<double>& M, device::DeviceContext& context);
422420

423421
/**
424-
* \fn void normalize_vector_cublas(Matrix<tsvd_float>& M, DeviceContext& context)
422+
* \fn void normalize_vector_cublas(Matrix<float>& M, device::DeviceContext& context)
425423
*
426424
* \brief Normalize a vector utilizing cuBLAS
427425
*
428426
* \param [in,out] M The vector to process
429427
* \param [in,out] context Device context.
430428
*/
431-
void normalize_vector_cublas(Matrix<tsvd_float>& M, DeviceContext& context);
429+
void normalize_vector_cublas(Matrix<float>& M, device::DeviceContext& context);
432430

433431
/**
434-
* \fn void normalize_vector_cublas(Matrix<tsvd_double>& M, DeviceContext& context)
432+
* \fn void normalize_vector_cublas(Matrix<double>& M, device::DeviceContext& context)
435433
*
436434
* \brief Normalize a vector utilizing cuBLAS
437435
*
438436
* \param [in,out] M The vector to process
439437
* \param [in,out] context Device context.
440438
*/
441-
void normalize_vector_cublas(Matrix<tsvd_double>& M, DeviceContext& context);
439+
void normalize_vector_cublas(Matrix<double>& M, device::DeviceContext& context);
442440

443441

444442
/**
445-
* \fn void normalize_vector_thrust(Matrix<tsvd_float>& M, DeviceContext& context)
443+
* \fn void normalize_vector_thrust(Matrix<float>& M, device::DeviceContext& context)
446444
*
447445
* \brief Normalize a vector utilizng Thrust
448446
*
@@ -451,45 +449,45 @@ namespace tsvd
451449
*/
452450

453451
template<typename T>
454-
void normalize_vector_thrust(Matrix<T>& M, DeviceContext& context);
452+
void normalize_vector_thrust(Matrix<T>& M, device::DeviceContext& context);
455453

456454
/**
457-
* \fn void residual(const Matrix<tsvd_float >&X, const Matrix<tsvd_float >&D, const Matrix<tsvd_float >&S, Matrix<tsvd_float >&R, DeviceContext & context);
455+
* \fn void residual(const Matrix<float >&X, const Matrix<float >&D, const Matrix<float >&S, Matrix<float >&R, device::DeviceContext & context);
458456
*
459457
* \brief Calculate residual R = X - DS
460458
*
461459
*/
462460

463-
void residual(const Matrix<tsvd_float>& X, const Matrix<tsvd_float>& D, const Matrix<tsvd_float>& S, Matrix<tsvd_float>& R, DeviceContext& context);
464-
void residual(const Matrix<tsvd_double>& X, const Matrix<tsvd_double>& D, const Matrix<tsvd_double>& S, Matrix<tsvd_double>& R, DeviceContext& context);
461+
void residual(const Matrix<float>& X, const Matrix<float>& D, const Matrix<float>& S, Matrix<float>& R, device::DeviceContext& context);
462+
void residual(const Matrix<double>& X, const Matrix<double>& D, const Matrix<double>& S, Matrix<double>& R, device::DeviceContext& context);
465463

466-
void calculate_eigen_pairs_exact(const Matrix<tsvd_float>& X, Matrix<tsvd_float>& Q, Matrix<tsvd_float>& w, DeviceContext& context);
467-
void calculate_eigen_pairs_exact(const Matrix<tsvd_double>& X, Matrix<tsvd_double>& Q, Matrix<tsvd_double>& w, DeviceContext& context);
464+
void calculate_eigen_pairs_exact(const Matrix<float>& X, Matrix<float>& Q, Matrix<float>& w, device::DeviceContext& context);
465+
void calculate_eigen_pairs_exact(const Matrix<double>& X, Matrix<double>& Q, Matrix<double>& w, device::DeviceContext& context);
468466

469-
void dot_product(Matrix<tsvd_float>& b_k1, Matrix<tsvd_float>& b_k, float* eigen_value_estimate, DeviceContext& context);
470-
void dot_product(Matrix<tsvd_double>& b_k1, Matrix<tsvd_double>& b_k, double* eigen_value_estimate, DeviceContext& context);
467+
void dot_product(Matrix<float>& b_k1, Matrix<float>& b_k, float* eigen_value_estimate, device::DeviceContext& context);
468+
void dot_product(Matrix<double>& b_k1, Matrix<double>& b_k, double* eigen_value_estimate, device::DeviceContext& context);
471469

472-
void max_index_per_column(Matrix<tsvd_float>& A, std::vector<int>& result_array, DeviceContext& context);
473-
void max_index_per_column(Matrix<tsvd_double>& A, std::vector<int>& result_array, DeviceContext& context);
470+
void max_index_per_column(Matrix<float>& A, std::vector<int>& result_array, device::DeviceContext& context);
471+
void max_index_per_column(Matrix<double>& A, std::vector<int>& result_array, device::DeviceContext& context);
474472

475473
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
476474
//Stricly floating point operations that are not used
477475

478476
/**
479-
* \fn void linear_solve(const Matrix<tsvd_float>& A, Matrix<tsvd_float>& X, const Matrix<tsvd_float>& B, DeviceContext& context)
477+
* \fn void linear_solve(const Matrix<float>& A, Matrix<float>& X, const Matrix<float>& B, device::DeviceContext& context)
480478
*
481479
* \brief Solve linear system AX=B to find B.
482480
*
483-
* \param A The Matrix&lt;tsvd_float&gt; to process.
484-
* \param [in,out] X The Matrix&lt;tsvd_float&gt; to process.
485-
* \param B The Matrix&lt;tsvd_float&gt; to process.
481+
* \param A The Matrix&lt;float&gt; to process.
482+
* \param [in,out] X The Matrix&lt;float&gt; to process.
483+
* \param B The Matrix&lt;float&gt; to process.
486484
* \param [in,out] context The context.
487485
*/
488486

489-
void linear_solve(const Matrix<tsvd_float>& A, Matrix<tsvd_float>& X, const Matrix<tsvd_float>& B, DeviceContext& context);
487+
void linear_solve(const Matrix<float>& A, Matrix<float>& X, const Matrix<float>& B, device::DeviceContext& context);
490488

491489
/**
492-
* \fn void pseudoinverse(const Matrix<tsvd_float>& A, Matrix<tsvd_float>& pinvA, DeviceContext& context)
490+
* \fn void pseudoinverse(const Matrix<float>& A, Matrix<float>& pinvA, device::DeviceContext& context)
493491
*
494492
* \brief Calculate Moore-Penrose seudoinverse using the singular value decomposition method.
495493
*
@@ -498,6 +496,6 @@ namespace tsvd
498496
* \param [in,out] context Device context.
499497
*/
500498

501-
void pseudoinverse(const Matrix<tsvd_float>& A, Matrix<tsvd_float>& pinvA, DeviceContext& context);
499+
void pseudoinverse(const Matrix<float>& A, Matrix<float>& pinvA, device::DeviceContext& context);
502500

503501
}

src/gpu/device/device_context.cuh

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#pragma once
22
#include "cublas_v2.h"
3-
#include "../tsvd/utils.cuh"
3+
#include "../utils/utils.cuh"
44
#include <cusparse.h>
55
#include <cusolverDn.h>
66

7-
namespace tsvd
7+
namespace device
88
{
9+
using namespace h2o4gpu;
10+
911
class DeviceContext
1012
{
1113
public:
1214
cublasHandle_t cublas_handle;
1315
cusolverDnHandle_t cusolver_handle;
1416
cusparseHandle_t cusparse_handle;
15-
CubMemory cub_mem;
17+
h2o4gpu::CubMemory cub_mem;
1618

1719
DeviceContext()
1820
{

0 commit comments

Comments
 (0)