-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnvmatrix.cu
1325 lines (1182 loc) · 55.1 KB
/
nvmatrix.cu
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nvmatrix.cu
*
* Created on: 20-Jan-2009
* Author: Alex Krizhevsky ([email protected])
*/
#include <assert.h>
#include <cublas.h>
#include <helper_cuda.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "nvmatrix.cuh"
using namespace std;
cudaDeviceProp NVMatrix::deviceProps;
unsigned int NVMatrix::hostRndMults[NUM_RND_STREAMS];
bool NVMatrix::rndInitialized = false;
/*
* Device random number generator pointers.
*/
unsigned int *NVMatrix::devRndMults;
unsigned long long *NVMatrix::devRndWords;
void NVMatrix::initDeviceProps() {
int deviceCount;
checkCudaErrors(cudaGetDeviceCount(&deviceCount));
if (deviceCount == 0) {
printf("There is no device supporting CUDA\n");
exit(EXIT_FAILURE);
}
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, 0));
}
void NVMatrix::_init(unsigned int numRows, unsigned int numCols) {
_numRows = numRows;
_numCols = numCols;
_numElements = numRows * numCols;
_ownsData = true;
/*
* By default, new matrices are in column-major order because that's how CUBLAS likes it.
*/
_isTrans = true;
_devData = NULL;
if (_numElements > 0) {
cublasAlloc(_numElements, sizeof(float), (void**) &_devData);
checkCublasError("!!!! device memory allocation error\n");
}
}
NVMatrix::NVMatrix() {
_init(0, 0);
}
NVMatrix::NVMatrix(bool isTrans) {
_init(0, 0);
setTrans(isTrans);
}
NVMatrix::NVMatrix(int numRows, int numCols, bool isTrans) {
_init(numRows, numCols);
setTrans(isTrans);
}
NVMatrix::NVMatrix(const NVMatrix& like, bool copy) {
_init(like.getNumRows(), like.getNumCols());
_isTrans = like.isTrans();
if(copy) {
copyFromDevice(like);
}
}
/*
* Initializes NVMatrix with same dimensions as given matrix but
* does not copy any data.
*/
NVMatrix::NVMatrix(const NVMatrix& like) {
_init(like.getNumRows(), like.getNumCols());
_isTrans = like.isTrans();
}
NVMatrix::NVMatrix(float* devData, int numRows, int numCols, bool isTrans) {
_numRows = numRows;
_numCols = numCols;
_numElements = numRows * numCols;
_ownsData = false;
_devData = devData;
_isTrans = isTrans;
}
NVMatrix::NVMatrix(bool fromHost, float* hostData, unsigned int numRows, unsigned int numCols) {
_init(numRows, numCols);
copyFromHost(hostData, numRows, numCols);
}
NVMatrix::NVMatrix(bool fromHost, float* hostData, unsigned int numRows, unsigned int numCols, bool isTrans) {
_init(numRows, numCols);
copyFromHost(hostData, numRows, numCols);
_isTrans = isTrans;
}
NVMatrix::~NVMatrix() {
if(_ownsData && _numElements > 0) {
cublasStatus status = cublasFree(_devData);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! memory free error\n");
exit(EXIT_FAILURE);
}
}
}
void NVMatrix::copyFromHost(const float* hostMatrix, unsigned int numRows, unsigned int numCols, bool resizeDeviceMatrix) {
if(resizeDeviceMatrix) {
resize(numRows, numCols);
}
copyFromHost(hostMatrix, numRows, numCols);
}
void NVMatrix::copyFromHost(const float* hostMatrix, unsigned int numRows, unsigned int numCols) {
assert(isSameDims(numRows, numCols));
cublasStatus status = cublasSetVector(_numElements, sizeof(float), hostMatrix, 1, _devData, 1);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! device access error (write)\n");
exit(EXIT_FAILURE);
}
_isTrans = true; // we now have Matlab format (= column-major)
}
void NVMatrix::copyFromDevice(const NVMatrix& devMatrix) {
assert(isSameDims(devMatrix));
cublasScopy(_numElements,devMatrix._devData, 1, _devData,1);
checkCublasError("cublasScopy failed");
_isTrans = devMatrix.isTrans();
}
void NVMatrix::copyFromDevice(const NVMatrix& devMatrix, bool resizeTarget) {
if (resizeTarget) {
resize(devMatrix);
}
copyFromDevice(devMatrix);
}
void NVMatrix::copyToHost(float* hostMatrix, unsigned int numRows, unsigned int numCols) const {
assert(isSameDims(numRows, numCols));
if(!_isTrans && (numRows > 1 || numCols > 1)) fprintf(stderr, "!!!! should have explicitly transposed the matrix before copying to Matlab host\n");
cublasStatus status = cublasGetVector(_numElements, sizeof(float), _devData, 1, hostMatrix, 1);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! device access error (read)\n");
exit(EXIT_FAILURE);
}
}
void NVMatrix::rightMult(const NVMatrix &b, float scaleAB, NVMatrix &target) const {
// assert(&target != &b);
assert(_numCols == b.getNumRows());
if(&target != this) {
target.resize(_numRows, b.getNumCols());
}
assert(target.getNumRows() == _numRows);
assert(target.getNumCols() == b.getNumCols());
if(_numRows % 64 != 0 || _numCols % 64 != 0 || b.getNumCols() % 64 != 0) {
WARN("Matrix dimensions not divisible by 64 -- cublasSgemm performance may suffer.");
}
cublasSgemm(getTransChar(), b.getTransChar(), _numRows, b.getNumCols(), _numCols,
scaleAB, _devData, getLeadingDim(), b.getDevData(), b.getLeadingDim(),
0, target.getDevData(), getNumRows());
checkCublasError("cublasSgemm failed");
target._isTrans = true; //because target is now in col-major order
}
void NVMatrix::rightMult(const NVMatrix &b, float scaleAB) {
rightMult(b, scaleAB, *this);
}
void NVMatrix::rightMult(const NVMatrix &b, NVMatrix& target) const {
rightMult(b, 1, target);
}
/*
* This will only work if this matrix is in column-major order! In other words,
* if isTrans() returns true.
*/
void NVMatrix::addProduct(const NVMatrix& a, const NVMatrix &b, float scaleThis, float scaleAB) {
assert(a.getNumCols() == b.getNumRows());
assert(this->getNumRows() == a.getNumRows());
assert(this->getNumCols() == b.getNumCols());
assert(_isTrans);
if(a.getNumRows() % 64 != 0 || a.getNumCols() % 64 != 0 || b.getNumCols() % 64 != 0) {
WARN("Matrix dimensions not divisible by 64 -- cublasSgemm performance may suffer.");
}
cublasSgemm(a.getTransChar(), b.getTransChar(), a.getNumRows(), b.getNumCols(), a.getNumCols(),
scaleAB, a.getDevData(), a.getLeadingDim(), b.getDevData(), b.getLeadingDim(),
scaleThis, _devData, getLeadingDim());
checkCublasError("cublasSgemm failed");
}
void NVMatrix::addProduct(const NVMatrix& a, const NVMatrix &b) {
addProduct(a, b, 1, 1);
}
void NVMatrix::apply(NVMatrix::FUNCTIONS f, NVMatrix& target, int numBlocks, int numThreadsPerBlock) {
target.resize(*this);
target._isTrans = _isTrans;
dim3 grid( numBlocks, 1, 1);
dim3 threads( numThreadsPerBlock, 1, 1);
if(f == NVMatrix::EXP) {
kExp<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::STUDENT) {
kStudent<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::LOGISTIC1) {
kLogistic1<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::LOGISTIC2) {
kLogistic2<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::SQUARE) {
kSquare<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::SQRT) {
kSqrt<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::ZERO) {
kZero<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if (f == NVMatrix::ONE) {
kOne<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if(f == NVMatrix::RECIPROCAL) {
kReciprocal<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if(f == NVMatrix::LOG) {
kLog<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if(f == NVMatrix::ABS) {
kAbs<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if(f == NVMatrix::SIGN) {
kSign<<<grid, threads>>>(_devData, target._devData, _numElements);
} else if(f == NVMatrix::SIGN) {
kStudent<<<grid, threads>>>(_devData, target._devData, _numElements);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::apply(NVMatrix::FUNCTIONS f, int numBlocks, int numThreadsPerBlock) {
apply(f, *this, numBlocks, numThreadsPerBlock);
}
/*
* The random number generator uses the multiply with carry algorithm. I got the
* multipliers from a site I can't find anymore.
*/
void NVMatrix::initRandom(unsigned int seed) {
assert(!rndInitialized);
ifstream inFile;
inFile.open(RND_MULTIPLIERS_FILE);
if(!inFile) {
std::cerr << "Unable to open file " << RND_MULTIPLIERS_FILE << std::endl;
exit(EXIT_FAILURE);
}
unsigned int mult;
for (int numRead = 0; numRead < NUM_RND_STREAMS; numRead++) {
if (!(inFile >> mult)) {
std::cerr << "Not enough numbers in file " << RND_MULTIPLIERS_FILE << std::endl;
exit(EXIT_FAILURE);
}
hostRndMults[numRead] = mult;
}
inFile.close();
checkCudaErrors(cudaMalloc((void **)&devRndMults, NUM_RND_STREAMS * sizeof(unsigned int)));
checkCudaErrors(cudaMalloc((void **)&devRndWords, NUM_RND_STREAMS * sizeof(unsigned long long)));
checkCudaErrors(cudaMemcpy(devRndMults, hostRndMults, NUM_RND_STREAMS * sizeof(unsigned int), cudaMemcpyHostToDevice));
kSeedRandom<<<NUM_RND_BLOCKS, NUM_RND_THREADS_PER_BLOCK>>>(devRndMults, devRndWords, seed);
getLastCudaError("Kernel execution failed");
rndInitialized = true;
}
void NVMatrix::destroyRandom() {
assert(rndInitialized);
checkCudaErrors(cudaFree(devRndMults));
checkCudaErrors(cudaFree(devRndWords));
rndInitialized = false;
}
void NVMatrix::binarizeProbs() {
assert(rndInitialized);
kBinarizeProbs<<<NUM_RND_BLOCKS,NUM_RND_THREADS_PER_BLOCK>>>(devRndMults, devRndWords, _devData,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::randomizeUniform() {
assert(rndInitialized);
kRandomUniform<<<NUM_RND_BLOCKS,NUM_RND_THREADS_PER_BLOCK>>>(devRndMults, devRndWords, _devData,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::randomizeGaussian() {
randomizeGaussian(1);
}
void NVMatrix::randomizeGaussian(float stdev) {
assert(rndInitialized);
kRandomGaussian<<<NUM_RND_BLOCKS,NUM_RND_THREADS_PER_BLOCK>>>(devRndMults, devRndWords, _devData, stdev, _numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::addGaussianNoise() {
addGaussianNoise(1);
}
void NVMatrix::addGaussianNoise(float stdev) {
assert(rndInitialized);
assert(_numElements % 2 == 0);
kAddGaussianNoise<<<NUM_RND_BLOCKS,NUM_RND_THREADS_PER_BLOCK>>>(devRndMults, devRndWords, _devData,stdev,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::biggerThanScalar(float scalar) {
biggerThanScalar(scalar, *this);
}
void NVMatrix::biggerThanScalar(float scalar, NVMatrix& target) {
kBiggerThanScalar<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData,scalar,target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::biggerThan(NVMatrix& m, NVMatrix& target, int numBlocks, int numThreadsPerBlock) {
assert(isSameDims(m));
target.resize(*this);
for (unsigned int elementsDone = 0; elementsDone < _numElements; elementsDone += numBlocks*numThreadsPerBlock) {
kBiggerThan<<<numBlocks, numThreadsPerBlock>>>(_devData + elementsDone,
m._devData + elementsDone, target._devData + elementsDone,
_numElements - elementsDone);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::biggerThan(NVMatrix& m, int numBlocks, int numThreadsPerBlock) {
biggerThan(m, *this, numBlocks, numThreadsPerBlock);
}
void NVMatrix::_checkBounds(int startRow, int endRow, int startCol, int endCol) const {
assert(startRow >= 0 && startRow <= _numRows);
assert(endRow >= 0 && endRow <= _numRows);
assert(startCol >= 0 && startCol <= _numCols);
assert(endCol >= 0 && endCol <= _numCols);
}
NVMatrix& NVMatrix::slice(int startRow, int endRow, int startCol, int endCol) const {
endRow = endRow < 0 ? this->_numRows : endRow;
endCol = endCol < 0 ? this->_numCols : endCol;
_checkBounds(startRow, endRow, startCol, endCol);
if (!isTrans() && ((startCol == 0 && endCol == this->_numCols) || startRow == endRow - 1)) {
return *new NVMatrix(this->_devData + startRow * this->_numCols + startCol, endRow - startRow, endCol - startCol, false);
} else if(isTrans() && ((startRow == 0 & endRow == this->_numRows) || startCol == endCol - 1)) {
return *new NVMatrix(this->_devData + startCol * this->_numRows + startRow, endRow - startRow, endCol - startCol, true);
}
WARN("Slice: result will not be a view.");
NVMatrix& newSlice = *new NVMatrix(endRow - startRow, endCol - startCol);
this->copy(newSlice, startRow, endRow, startCol, endCol, 0, 0);
return newSlice;
}
/* this will NEVER return a view */
void NVMatrix::slice(int startRow, int endRow, int startCol, int endCol, NVMatrix& target) const {
endRow = endRow < 0 ? this->_numRows : endRow;
endCol = endCol < 0 ? this->_numCols : endCol;
_checkBounds(startRow, endRow, startCol, endCol);
target.resize(endRow - startRow, endCol - startCol);
target._isTrans = _isTrans;
this->copy(target, startRow, endRow, startCol, endCol, 0, 0);
}
NVMatrix& NVMatrix::sliceRows(int startRow, int endRow) const {
return slice(startRow, endRow, 0, -1);
}
void NVMatrix::sliceRows(int startRow, int endRow, NVMatrix& target) const {
slice(startRow, endRow, 0, -1, target);
}
NVMatrix& NVMatrix::sliceCols(int startCol, int endCol) const {
return slice(0, -1, startCol, endCol);
}
void NVMatrix::sliceCols(int startCol, int endCol, NVMatrix& target) const {
slice(0, -1, startCol, endCol, target);
}
/*
* Guaranteed to not change the data if the number of elements doesn't change.
* So you can use this to "reshape" a matrix.
*/
bool NVMatrix::resize(int numRows, int numCols) {
bool reallocated = false;
if (numRows != _numRows || numCols != _numCols) {
assert(_ownsData);
if (_numElements != numRows * numCols) {
cublasStatus status = cublasFree(_devData);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! memory free error\n");
exit(EXIT_FAILURE);
}
status = cublasAlloc(numCols * numRows, sizeof(float), (void**) &_devData);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! device memory allocation error\n");
exit(EXIT_FAILURE);
}
reallocated = true;
}
_numRows = numRows;
_numCols = numCols;
_numElements = numRows * numCols;
}
return reallocated;
}
bool NVMatrix::resize(const NVMatrix& like) {
bool r = resize(like.getNumRows(), like.getNumCols());
_isTrans = like._isTrans;
return r;
}
void NVMatrix::reshape(int numRows, int numCols) {
assert(_numElements == numRows*numCols);
_numRows = numRows;
_numCols = numCols;
}
NVMatrix& NVMatrix::reshaped(int numRows, int numCols) {
assert(_numElements == numRows*numCols);
return *new NVMatrix(_devData, numRows, numCols, _isTrans);
}
void NVMatrix::copy(NVMatrix &dest, int srcStartRow, int srcEndRow,
int srcStartCol, int srcEndCol,
int destStartRow, int destStartCol) const {
srcEndRow = srcEndRow < 0 ? this->_numRows : srcEndRow;
srcEndCol = srcEndCol < 0 ? this->_numCols : srcEndCol;
assert(destStartRow >= 0 && destStartCol >= 0); //some range-checking
assert(srcEndRow <= _numRows && srcEndCol <= _numCols);
assert(destStartRow + srcEndRow - srcStartRow <= dest.getNumRows());
assert(destStartCol + srcEndCol - srcStartCol <= dest.getNumCols());
const int srcJumpWidth = !_isTrans ? getNumCols() : getNumRows();
const int destJumpWidth = !dest._isTrans ? dest.getNumCols() : dest.getNumRows();
float* srcStartPtr = getCellPtr(srcStartRow, srcStartCol);
float* destStartPtr = dest.getCellPtr(destStartRow, destStartCol);
if (isTrans() != dest.isTrans()) {
// copyWidth here refers to dest
const int copyWidth = !dest._isTrans ? srcEndCol - srcStartCol : srcEndRow - srcStartRow;
const int copyHeight = !dest._isTrans ? srcEndRow - srcStartRow : srcEndCol - srcStartCol;
//call copy kernel for transposed matrices
// const int width = dest.isTrans() ? _numRows : _numCols;
// const int height = dest.isTrans() ? _numCols : _numRows;
const bool checkBounds = !(copyWidth % ADD_BLOCK_SIZE == 0 && copyHeight % ADD_BLOCK_SIZE == 0);
const int numBlocksX = DIVUP(copyWidth, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::max(1, std::min(DIVUP(copyHeight, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsCopied = 0;
// printf("calling kCopyTransFast\n");
while (numRowsCopied < copyHeight) {
if (checkBounds) {
kCopyTransFast<true><<<gridSize, blockSize>>>(&destStartPtr[numRowsCopied * destJumpWidth],
&srcStartPtr[numRowsCopied], copyWidth, copyHeight - numRowsCopied, destJumpWidth, srcJumpWidth);
} else {
kCopyTransFast<false><<<gridSize, blockSize>>>(&destStartPtr[numRowsCopied * destJumpWidth],
&srcStartPtr[numRowsCopied], copyWidth, copyHeight - numRowsCopied, destJumpWidth, srcJumpWidth);
}
getLastCudaError("copy: Kernel execution failed");
numRowsCopied += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::max(1, std::min(DIVUP(copyHeight-numRowsCopied, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
}
} else {
// copyWidth here refers to src
const int copyWidth = !_isTrans ? srcEndCol - srcStartCol : srcEndRow - srcStartRow;
const int copyHeight = !_isTrans ? srcEndRow - srcStartRow : srcEndCol - srcStartCol;
const int numToCopy = copyWidth * copyHeight;
kCopy<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(srcStartPtr, destStartPtr, copyWidth, srcJumpWidth, destJumpWidth, numToCopy);
}
}
NVMatrix& NVMatrix::getTranspose() {
NVMatrix* trans = new NVMatrix(_devData, _numCols, _numRows, !_isTrans);
return *trans;
}
/*
* Flips the ordering of the matrix from row-major to column-major and vice versa.
* This creates temporary storage -- not a cheap operation.
*
* This is not equivalent to a "hard transpose". The resultant matrix still has
* the same dimensions, its layout in memory just changes.
*/
void NVMatrix::flipTrans() {
NVMatrix* meTrans = new NVMatrix(*this);
// assert(_numCols % ADD_BLOCK_SIZE == 0 && _numRows % ADD_BLOCK_SIZE == 0);
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
const int numBlocksY = DIVUP(height, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX && numBlocksY < NUM_BLOCKS_MAX);
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
kTranspose<<<gridSize, blockSize>>>(_devData, meTrans->_devData, width, height);
getLastCudaError("Kernel execution failed");
copyFromDevice(*meTrans);
this->_isTrans = !this->_isTrans;
delete meTrans;
}
void NVMatrix::squaredDiff(NVMatrix& b) {
squaredDiff(b, *this);
}
void NVMatrix::squaredDiff(NVMatrix& b, NVMatrix& target) {
assert(this->isSameDims(b));
assert(&target != &b);
target.resize(*this);
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
if (_isTrans != b._isTrans) {
const bool checkBounds = !(width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0);
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::max(1, std::min(DIVUP(height, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsAdded = 0;
float* aData = _devData, *bData = b._devData, *destData = target._devData;
// printf("calling trans sq diff\n");
while (numRowsAdded < height) {
if(checkBounds) {
kSquaredDiffTransFast<true><<<gridSize, blockSize>>>(aData, bData, destData, width, height - numRowsAdded, height);
} else {
kSquaredDiffTransFast<false><<<gridSize, blockSize>>>(aData, bData, destData, width, height - numRowsAdded, height);
}
getLastCudaError("Kernel execution failed");
numRowsAdded += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::max(1, std::min(DIVUP(height-numRowsAdded, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
aData += numRowsAdded * width;
bData += b._isTrans != _isTrans ? numRowsAdded : numRowsAdded * width;
destData += numRowsAdded * width;
}
} else {
// printf("calling plain sq diff\n");
kSquaredDiff<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, b._devData, target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
}
void NVMatrix::addSum(NVMatrix& b, NVMatrix& c, float scaleThis, float scaleB, float scaleC) {
assert(this->isSameDims(b));
assert(this->isSameDims(c));
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
if((_isTrans != b._isTrans || _isTrans != c._isTrans) && std::min(_numRows, _numCols) > 1) {
bool checkBounds = !(width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0);
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::max(1, std::min(DIVUP(height, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsAdded = 0;
float* aData = _devData, *bData = b._devData, *cData = c._devData;
const bool transB = b._isTrans != _isTrans, transC = c._isTrans != _isTrans;
while (numRowsAdded < height) {
if(transB) {
if(transC) {
if(checkBounds) {
kAddTrans3Fast<true, true, true><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
} else {
kAddTrans3Fast<false, true, true><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
}
} else {
if(checkBounds) {
kAddTrans3Fast<true, true, false><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
} else {
kAddTrans3Fast<false, true, false><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
}
}
} else {
if(transC) {
if(checkBounds) {
kAddTrans3Fast<true, false, true><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
} else {
kAddTrans3Fast<false, false, true><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
}
} else {
if(checkBounds) {
kAddTrans3Fast<true, false, false><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
} else {
kAddTrans3Fast<false, false, false><<<gridSize, blockSize>>>(aData, bData, cData,width, height - numRowsAdded, height,
scaleThis, scaleB, scaleC);
}
}
}
getLastCudaError("Kernel execution failed");
numRowsAdded += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::max(1, std::min(DIVUP((height-numRowsAdded) , ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
aData += numRowsAdded * width;
bData += b._isTrans != _isTrans ? numRowsAdded : numRowsAdded * width;
cData += c._isTrans != _isTrans ? numRowsAdded : numRowsAdded * width;
}
} else {
kAdd3<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, b._devData, c._devData,
_numElements, scaleThis, scaleB, scaleC);
getLastCudaError("Kernel execution failed");
}
}
void NVMatrix::add(NVMatrix& b, float scaleA, float scaleB, NVMatrix& target) {
if(&target == &b && &target != this) { // because we manipulate target to be like a
b.add(*this, scaleB, scaleA);
return;
}
assert(this->isSameDims(b));
target.resize(*this);
if (isTrans() != b.isTrans() && std::min(_numRows, _numCols) > 1) {
//call addition kernel for transposed matrices
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
const bool checkBounds = !(width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0);
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::max(1, std::min(DIVUP(height, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsAdded = 0;
while (numRowsAdded < height) {
if (checkBounds) {
kAddTransFast<true><<<gridSize, blockSize>>>(&_devData[numRowsAdded * width],
&b._devData[numRowsAdded], &target._devData[numRowsAdded * width],
width, height - numRowsAdded, height, scaleA, scaleB);
} else {
kAddTransFast<false><<<gridSize, blockSize>>>(&_devData[numRowsAdded * width],
&b._devData[numRowsAdded], &target._devData[numRowsAdded * width],
width, height - numRowsAdded, height, scaleA, scaleB);
}
getLastCudaError("Kernel execution failed");
numRowsAdded += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::max(1, std::min(DIVUP(height-numRowsAdded, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX));
}
} else {
if(scaleA == 1.0f) {
cublasSaxpy(_numElements, scaleB, b._devData, 1, target._devData, 1);
checkCublasError("cublasSaxpy failed");
} else {
kAdd<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, b._devData, target._devData,
_numElements, scaleA, scaleB);
}
}
}
void NVMatrix::add(NVMatrix& b, float scaleB, NVMatrix& target) {
add(b, 1, scaleB, target);
}
void NVMatrix::add(NVMatrix& b, NVMatrix& target) {
add(b, 1, target);
}
void NVMatrix::add(NVMatrix& b, float scaleB) {
add(b, scaleB, *this);
}
void NVMatrix::add(NVMatrix& b, float scaleA, float scaleB) {
add(b, scaleA, scaleB, *this);
}
void NVMatrix::add(NVMatrix& b) {
add(b, 1, *this);
}
void NVMatrix::subtract(NVMatrix& b, NVMatrix& target) {
add(b, -1, target);
}
void NVMatrix::subtract(NVMatrix& b) {
add(b, -1);
}
void NVMatrix::eltWiseMult(NVMatrix& b, NVMatrix& target) {
if(&target == &b && &target != this) { // because we manipulate target to be like a
b.eltWiseMult(*this);
return;
}
assert(this->isSameDims(b));
target.resize(*this);
if (isTrans() != b.isTrans() && std::min(_numRows, _numCols) > 1) {
//call mult kernel for transposed matrices
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
const bool checkBounds = !(width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0);
// if (width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0) {
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::min(DIVUP(height, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX);
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsProcessed = 0;
while (numRowsProcessed < height) {
if (checkBounds) {
kMultTransFast<true><<<gridSize, blockSize>>>(&_devData[numRowsProcessed * width],
&b._devData[numRowsProcessed], &target._devData[numRowsProcessed * width],
width, height - numRowsProcessed, height);
} else {
kMultTransFast<false><<<gridSize, blockSize>>>(&_devData[numRowsProcessed * width],
&b._devData[numRowsProcessed], &target._devData[numRowsProcessed * width],
width, height - numRowsProcessed, height);
}
getLastCudaError("Kernel execution failed");
numRowsProcessed += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::min(DIVUP(height-numRowsProcessed, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX);
}
// }
} else {
kMult<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, b._devData, target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
}
void NVMatrix::eltWiseMult(NVMatrix& b) {
eltWiseMult(b, *this);
}
void NVMatrix::eltWiseDivide(NVMatrix& b, NVMatrix& target) {
assert(&b != this); // doable but not necessary for me
assert(this->isSameDims(b));
target.resize(*this);
if (isTrans() != b.isTrans() && std::min(_numRows, _numCols) > 1) {
//call mult kernel for transposed matrices
const int width = isTrans() ? _numRows : _numCols;
const int height = isTrans() ? _numCols : _numRows;
const bool checkBounds = !(width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0);
// if (width % ADD_BLOCK_SIZE == 0 && height % ADD_BLOCK_SIZE == 0) {
const int numBlocksX = DIVUP(width, ADD_BLOCK_SIZE);
assert(numBlocksX < NUM_BLOCKS_MAX);
const int numBlocksY = std::min(height / ADD_BLOCK_SIZE, NUM_BLOCKS_MAX);
dim3 gridSize(numBlocksX, numBlocksY, 1);
dim3 blockSize(ADD_BLOCK_SIZE, ADD_BLOCK_SIZE, 1);
int numRowsProcessed = 0;
while (numRowsProcessed < height) {
if (checkBounds) {
kDivideTransFast<true><<<gridSize, blockSize>>>(&_devData[numRowsProcessed * width],
&b._devData[numRowsProcessed], &target._devData[numRowsProcessed * width],
width, height - numRowsProcessed, height);
} else {
kDivideTransFast<false><<<gridSize, blockSize>>>(&_devData[numRowsProcessed * width],
&b._devData[numRowsProcessed], &target._devData[numRowsProcessed * width],
width, height - numRowsProcessed, height);
}
getLastCudaError("Kernel execution failed");
numRowsProcessed += gridSize.y * ADD_BLOCK_SIZE;
gridSize.y = std::min(DIVUP(height-numRowsProcessed, ADD_BLOCK_SIZE), NUM_BLOCKS_MAX);
}
// }
} else {
kDivide<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, b._devData, target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
}
void NVMatrix::eltWiseDivide(NVMatrix& b) {
eltWiseDivide(b, *this);
}
void NVMatrix::tile(int timesY, int timesX, NVMatrix& target) {
assert(timesX > 0 && timesY > 0);
target.resize(_numRows*timesY, _numCols*timesX);
target._isTrans = _isTrans;
if(!isTrans()) {
kTile<<<NUM_APPLY_BLOCKS,NUM_APPLY_THREADS_PER_BLOCK>>>(_devData, target._devData, _numCols, _numRows, target._numCols, target._numRows);
} else {
kTile<<<NUM_APPLY_BLOCKS,NUM_APPLY_THREADS_PER_BLOCK>>>(_devData, target._devData, _numRows, _numCols, target._numRows, target._numCols);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::addVector(NVMatrix& vec, float scaleVec, NVMatrix& target) {
if(&target == &vec && &target != this) { // because we manipulate target to be like this
vec.add(*this, scaleVec, 1);
return;
}
assert(vec.getNumRows() == 1 || vec.getNumCols() == 1);
assert(vec.getNumRows() == _numRows || vec.getNumCols() == _numCols);
// assert(&target != &vec);
target.resize(*this);
// const unsigned int numThreads = numBlocks*numThreadsPerBlock;
const unsigned int width = _isTrans ? _numRows : _numCols;
const unsigned int height = _isTrans ? _numCols : _numRows;
if(vec.getNumRows() == _numRows && !isTrans() || vec.getNumCols() == _numCols && isTrans()) {
kAddColVector<<<NUM_ADD_VECTOR_BLOCKS,NUM_ADD_VECTOR_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height, scaleVec);
} else {
kAddRowVector<<<NUM_ADD_VECTOR_BLOCKS,NUM_ADD_VECTOR_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height, scaleVec);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::addVector(NVMatrix& vec) {
addVector(vec, 1, *this);
}
void NVMatrix::addVector(NVMatrix& vec, float scaleVec) {
addVector(vec, scaleVec, *this);
}
void NVMatrix::addVector(NVMatrix& vec, NVMatrix& target) {
addVector(vec, 1, target);
}
void NVMatrix::equalsVector(NVMatrix& vec, NVMatrix& target) {
if(&target == &vec && &target != this) { // because we manipulate target to be like this
vec.equalsVector(*this);
return;
}
assert(vec.getNumRows() == 1 || vec.getNumCols() == 1);
assert(vec.getNumRows() == _numRows || vec.getNumCols() == _numCols);
target.resize(*this);
const unsigned int width = _isTrans ? _numRows : _numCols;
const unsigned int height = _isTrans ? _numCols : _numRows;
if(vec.getNumRows() == _numRows && !isTrans() || vec.getNumCols() == _numCols && isTrans()) {
kEqualsColVector<<<NUM_ADD_VECTOR_BLOCKS,NUM_ADD_VECTOR_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
} else {
kEqualsRowVector<<<NUM_ADD_VECTOR_BLOCKS,NUM_ADD_VECTOR_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::equalsVector(NVMatrix& vec) {
equalsVector(vec, *this);
}
void NVMatrix::subtractFromScalar(float scalar, NVMatrix& target) {
target.resize(*this);
kSubtractFromScalar<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, scalar, target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::subtractFromScalar(float scalar) {
subtractFromScalar(scalar, *this);
}
void NVMatrix::addScalar(float scalar, NVMatrix& target) {
target.resize(*this);
kAddScalar<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, scalar, target._devData,_numElements);
getLastCudaError("Kernel execution failed");
}
void NVMatrix::addScalar(float scalar) {
addScalar(scalar, *this);
}
void NVMatrix::eltWiseMultByVector(NVMatrix& vec, NVMatrix& target) {
assert(&target != &vec); // for now
assert(vec.getNumRows() == 1 || vec.getNumCols() == 1);
assert(vec.getNumRows() == _numRows || vec.getNumCols() == _numCols);
// assert(&target != &vec);
target.resize(*this);
target._isTrans = _isTrans;
// const unsigned int numThreads = numBlocks*numThreadsPerBlock;
const unsigned int width = _isTrans ? _numRows : _numCols;
const unsigned int height = _isTrans ? _numCols : _numRows;
if(vec.getNumRows() == _numRows && !isTrans() || vec.getNumCols() == _numCols && isTrans()) {
kMultByColVector<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
} else {
kMultByRowVector<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::eltWiseMultByVector(NVMatrix& vec) {
eltWiseMultByVector(vec, *this);
}
void NVMatrix::eltWiseDivideByVector(NVMatrix& vec) {
eltWiseDivideByVector(vec, *this);
}
void NVMatrix::eltWiseDivideByVector(NVMatrix& vec, NVMatrix& target) {
NVMatrix* vecRecip = new NVMatrix(vec);
vec.apply(NVMatrix::RECIPROCAL, *vecRecip);
eltWiseMultByVector(*vecRecip, target);
cudaThreadSynchronize();
delete vecRecip;
}
void NVMatrix::eltWiseDivideByVector2(NVMatrix& vec) {
eltWiseDivideByVector2(vec, *this);
}
void NVMatrix::eltWiseDivideByVector2(NVMatrix& vec, NVMatrix& target) {
assert(&target != &vec); // for now
assert(vec.getNumRows() == 1 || vec.getNumCols() == 1);
assert(vec.getNumRows() == _numRows || vec.getNumCols() == _numCols);
// assert(&target != &vec);
target.resize(*this);
// const unsigned int numThreads = numBlocks*numThreadsPerBlock;
const unsigned int width = _isTrans ? _numRows : _numCols;
const unsigned int height = _isTrans ? _numCols : _numRows;
if(vec.getNumRows() == _numRows && !isTrans() || vec.getNumCols() == _numCols && isTrans()) {
kDivideByColVector<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
} else {
kDivideByRowVector<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, vec._devData, target._devData, width, height);
}
getLastCudaError("Kernel execution failed");
}
void NVMatrix::scale(float scale) {
cublasSscal(_numElements, scale, _devData, 1);
checkCublasError("cublasSscal failed.");
}
void NVMatrix::scale(float scale, NVMatrix& target) {
target.resize(*this);
target.copyFromDevice(*this);
target.scale(scale);
}
void NVMatrix::setCell(int row_no, int col_no, float value) {
assert(row_no >= 0);
assert(col_no >= 0);
assert(row_no < _numRows);
assert(col_no < _numCols);
NVMatrix* value_mat = new NVMatrix(true, &value, 1, 1);
if(isTrans()) {
kSetElement<<<1, 1>>>(_devData, row_no, col_no, value_mat->getDevData(), _numCols, _numRows);
}
else {
kSetElement<<<1, 1>>>(_devData, col_no, row_no, value_mat->getDevData(), _numRows, _numCols);
}
delete value_mat;
}
float NVMatrix::getCell(int row_no, int col_no) {
assert(row_no >= 0);
assert(col_no >= 0);
assert(row_no < _numRows);
assert(col_no < _numCols);
// Copy element to new matrix
NVMatrix* value_mat = new NVMatrix(1, 1);
if(isTrans()) {
kGetElement<<<1, 1>>>(_devData, row_no, col_no, value_mat->getDevData(), _numCols, _numRows);
}
else {
kGetElement<<<1, 1>>>(_devData, col_no, row_no, value_mat->getDevData(), _numRows, _numCols);
}
// Copy to host and return
float value;
value_mat->copyToHost(&value, 1, 1);
delete value_mat;
return value;
}
void NVMatrix::setRow(NVMatrix& row, int row_no, float scaleRow, NVMatrix& target) {
target.resize(*this);
target.copyFromDevice(*this);
setRow(row, row_no, scaleRow);
}
void NVMatrix::setRow(NVMatrix& row, int row_no, float scaleRow) {
assert(_numCols == row.getNumCols());
assert(row.getNumRows() == 1);
assert(row_no < _numRows);
assert(row_no >= 0);
if(_isTrans)
kSetCol<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, row.getDevData(), row_no, scaleRow, _numRows, _numCols);
else
kSetRow<<<NUM_VECTOR_OP_BLOCKS,NUM_VECTOR_OP_THREADS_PER_BLOCK>>>(_devData, row.getDevData(), row_no, scaleRow, _numCols, _numRows);
}
void NVMatrix::setRow(NVMatrix& row, int row_no, NVMatrix& target) {
setRow(row, row_no, 1.0f, target);
}
void NVMatrix::setRow(NVMatrix& row, int row_no) {
setRow(row, row_no, 1.0f);
}
void NVMatrix::setCol(NVMatrix& col, int col_no, float scaleCol, NVMatrix& target) {