-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathVectorHipKernels.cpp
More file actions
1209 lines (1048 loc) · 42.1 KB
/
VectorHipKernels.cpp
File metadata and controls
1209 lines (1048 loc) · 42.1 KB
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
// Copyright (c) 2017, Lawrence Livermore National Security, LLC.
// Produced at the Lawrence Livermore National Laboratory (LLNL).
// LLNL-CODE-742473. All rights reserved.
//
// This file is part of HiOp. For details, see https://github.com/LLNL/hiop. HiOp
// is released under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause).
// Please also read "Additional BSD Notice" below.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// i. Redistributions of source code must retain the above copyright notice, this list
// of conditions and the disclaimer below.
// ii. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the disclaimer (as noted below) in the documentation and/or
// other materials provided with the distribution.
// iii. Neither the name of the LLNS/LLNL nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Additional BSD Notice
// 1. This notice is required to be provided under our contract with the U.S. Department
// of Energy (DOE). This work was produced at Lawrence Livermore National Laboratory under
// Contract No. DE-AC52-07NA27344 with the DOE.
// 2. Neither the United States Government nor Lawrence Livermore National Security, LLC
// nor any of their employees, makes any warranty, express or implied, or assumes any
// liability or responsibility for the accuracy, completeness, or usefulness of any
// information, apparatus, product, or process disclosed, or represents that its use would
// not infringe privately-owned rights.
// 3. Also, reference herein to any specific commercial products, process, or services by
// trade name, trademark, manufacturer or otherwise does not necessarily constitute or
// imply its endorsement, recommendation, or favoring by the United States Government or
// Lawrence Livermore National Security, LLC. The views and opinions of authors expressed
// herein do not necessarily state or reflect those of the United States Government or
// Lawrence Livermore National Security, LLC, and shall not be used for advertising or
// product endorsement purposes.
/**
* @file VectorHipKernels.cpp
*
* @author Nai-Yuan Chiang <chiang7@llnl.gov>, LLNL
*
*/
#include "VectorHipKernels.hpp"
#include <hip/hip_runtime.h>
#if HIP_VERSION >= 50200000
#include <hipblas/hipblas.h>
#else
#include <hipblas.h>
#endif
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/transform_reduce.h>
#include <thrust/extrema.h>
#include <thrust/logical.h>
#include <thrust/execution_policy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_free.h>
// #include <cmath>
// #include <limits>
/// @brief compute abs(b-a)
template<typename T>
struct thrust_abs_diff : public thrust::binary_function<T, T, T>
{
__host__ __device__ T operator()(const T& a, const T& b) { return fabs(b - a); }
};
/// @brief compute abs(a)
template<typename T>
struct thrust_abs : public thrust::unary_function<T, T>
{
__host__ __device__ T operator()(const T& a) { return fabs(a); }
};
/// @brief return true if abs(a) < tol_
struct thrust_abs_less
{
const double tol_;
thrust_abs_less(double tol)
: tol_(tol)
{}
__host__ __device__ int operator()(const double& a) { return (fabs(a) < tol_); }
};
/// @brief return true if a < tol_
struct thrust_less
{
const double tol_;
thrust_less(double tol)
: tol_(tol)
{}
__host__ __device__ int operator()(const double& a) { return (a < tol_); }
};
/// @brief return true if (0.0 < a) - (a < 0.0)
template<typename T>
struct thrust_sig : public thrust::unary_function<T, T>
{
__host__ __device__ T operator()(const T& a) { return static_cast<double>((0.0 < a) - (a < 0.0)); }
};
/// @brief compute sqrt(a)
template<typename T>
struct thrust_sqrt : public thrust::unary_function<T, T>
{
__host__ __device__ T operator()(const T& a) { return sqrt(a); }
};
/// @brief compute log(a) if a > 0, otherwise returns 0
template<typename T>
struct thrust_log_select : public thrust::unary_function<T, double>
{
__host__ __device__ double operator()(const T& a)
{
if(a > 0) {
return log(a);
}
return 0.;
}
};
/// @brief compute isinf(a)
template<typename T>
struct thrust_isinf : public thrust::unary_function<T, bool>
{
__host__ __device__ bool operator()(const T& a) { return isinf(a); }
};
/// @brief compute isfinite(a)
template<typename T>
struct thrust_isfinite : public thrust::unary_function<T, bool>
{
__host__ __device__ bool operator()(const T& a) { return isfinite(a); }
};
/// @brief compute a==0.0
template<typename T>
struct thrust_iszero : public thrust::unary_function<T, bool>
{
__host__ __device__ bool operator()(const T& a) { return a == (T)(0.0); }
};
/// @brief compute isnan(a)
template<typename T>
struct thrust_isnan : public thrust::unary_function<T, bool>
{
__host__ __device__ bool operator()(const T& a) { return isnan(a); }
};
/// @brief compute (bool) (a)
struct thrust_istrue : public thrust::unary_function<int, bool>
{
__host__ __device__ bool operator()(const int& a) { return a != 0; }
};
/** @brief Set y[i] = min(y[i],c), for i=[0,n_local-1] */
__global__ void component_min_hip(int n, double* y, const double c)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
y[i] = (y[i] < c) ? y[i] : c;
}
}
/** @brief Set y[i] = min(y[i],x[i]), for i=[0,n_local-1] */
__global__ void component_min_hip(int n, double* y, const double* x)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
y[i] = (y[i] < x[i]) ? y[i] : x[i];
}
}
/** @brief Set y[i] = max(y[i],c), for i=[0,n_local-1] */
__global__ void component_max_hip(int n, double* y, const double c)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
y[i] = (y[i] > c) ? y[i] : c;
}
}
/** @brief Set y[i] = max(y[i],x[i]), for i=[0,n_local-1] */
__global__ void component_max_hip(int n, double* y, const double* x)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
y[i] = (y[i] > x[i]) ? y[i] : x[i];
}
}
/// @brief Copy from src the elements specified by the indices in id.
__global__ void copy_from_index_hip(int n, double* vec, const double* val, const int* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
vec[i] = val[id[i]];
}
}
/// @brief Performs axpy, y += alpha*x, on the indexes in this specified by id.
__global__ void axpy_w_map_hip(int n, double* yd, const double* xd, const int* id, double alpha)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
assert(id[i] < n);
yd[id[i]] = alpha * xd[i] + yd[id[i]];
}
}
/** @brief this[i] += alpha*x[i]*z[i] forall i */
__global__ void axzpy_hip(int n, double* yd, const double* xd, const double* zd, double alpha)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
yd[i] = alpha * xd[i] * zd[i] + yd[i];
}
}
/** @brief this[i] += alpha*x[i]/z[i] forall i */
__global__ void axdzpy_hip(int n, double* yd, const double* xd, const double* zd, double alpha)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
yd[i] = alpha * xd[i] / zd[i] + yd[i];
}
}
/** @brief this[i] += alpha*x[i]/z[i] forall i with pattern selection */
__global__ void axdzpy_w_pattern_hip(int n, double* yd, const double* xd, const double* zd, const double* id, double alpha)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 1.0) {
yd[i] = alpha * xd[i] / zd[i] + yd[i];
}
}
}
/** @brief y[i] += alpha*1/x[i] + y[i] forall i with pattern selection */
__global__ void adxpy_w_pattern_hip(int n, double* yd, const double* xd, const double* id, double alpha)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 1.0) {
yd[i] = alpha / xd[i] + yd[i];
}
}
}
/** @brief elements of this that corespond to nonzeros in ix are divided by elements of v.
The rest of elements of this are set to zero.*/
__global__ void component_div_w_pattern_hip(int n, double* yd, const double* xd, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 1.0) {
yd[i] = yd[i] / xd[i];
} else {
yd[i] = 0.0;
}
}
}
/** @brief y[i] += c forall i */
__global__ void add_constant_hip(int n, double* yd, double c)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
yd[i] = yd[i] + c;
}
}
/** @brief y[i] += c forall i with pattern selection */
__global__ void add_constant_w_pattern_hip(int n, double* yd, double c, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
yd[i] = yd[i] + c * id[i];
}
}
/// @brief Invert (1/x) the elements of this
__global__ void invert_hip(int n, double* yd)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
yd[i] = 1. / yd[i];
}
}
/** @brief Linear damping term */
__global__ void set_linear_damping_term_hip(int n, double* yd, const double* vd, const double* ld, const double* rd)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(ld[i] == 1.0 && rd[i] == 0.0) {
yd[i] = vd[i];
} else {
yd[i] = 0.0;
}
}
}
/**
* @brief Performs `this[i] = alpha*this[i] + sign*ct` where sign=1 when EXACTLY one of
* ixleft[i] and ixright[i] is 1.0 and sign=0 otherwise.
*/
__global__ void add_linear_damping_term_hip(int n,
double* data,
const double* ixl,
const double* ixr,
double alpha,
double ct)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
data[i] = alpha * data[i] + ct * (ixl[i] - ixr[i]);
}
}
/** @brief y[i] = 1.0 if x[i] is positive and id[i] = 1.0, otherwise y[i] = 0 */
__global__ void is_posive_w_pattern_hip(int n, double* data, const double* vd, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
data[i] = (id[i] == 1.0 && vd[i] > 0.0) ? 1 : 0;
}
}
/** @brief y[i] = x[i] if id[i] = 1.0, otherwise y[i] = val_else */
__global__ void set_val_w_pattern_hip(int n, double* data, const double* vd, const double* id, double val_else)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
data[i] = (id[i] == 1.0) ? vd[i] : val_else;
}
}
/** @brief data[i] = 0 if id[i]==0.0 */
__global__ void select_pattern_hip(int n, double* data, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 0.0) {
data[i] = 0.0;
}
}
}
__global__ void match_pattern_hip(int n, double* data, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 0.0) {
data[i] = 0.0;
}
}
}
/** @brief Project solution into bounds */
__global__ void project_into_bounds_hip(int n,
double* xd,
const double* xld,
const double* ild,
const double* xud,
const double* iud,
double kappa1,
double kappa2,
double small_real)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
double aux = 0.0;
double aux2 = 0.0;
if(ild[i] != 0.0 && iud[i] != 0.0) {
aux = kappa2 * (xud[i] - xld[i]) - small_real;
aux2 = xld[i] + fmin(kappa1 * fmax(1.0, fabs(xld[i])), aux);
if(xd[i] < aux2) {
xd[i] = aux2;
} else {
aux2 = xud[i] - fmin(kappa1 * fmax(1.0, fabs(xud[i])), aux);
if(xd[i] > aux2) {
xd[i] = aux2;
}
}
#ifdef HIOP_DEEPCHECKS
assert(xd[i] > xld[i] && xd[i] < xud[i] && "this should not happen -> HiOp bug");
#endif
} else {
if(ild[i] != 0.0) {
xd[i] = fmax(xd[i], xld[i] + kappa1 * fmax(1.0, fabs(xld[i])) - small_real);
}
if(iud[i] != 0.0) {
xd[i] = fmin(xd[i], xud[i] - kappa1 * fmax(1.0, fabs(xud[i])) - small_real);
} else {
/*nothing for free vars */
}
}
}
}
/** @brief max{a\in(0,1]| x+ad >=(1-tau)x} */
__global__ void fraction_to_the_boundry_hip(int n, double* yd, const double* xd, const double* dd, double tau)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(dd[i] >= 0) {
yd[i] = 1.0;
} else {
yd[i] = -tau * xd[i] / dd[i];
}
}
}
/** @brief max{a\in(0,1]| x+ad >=(1-tau)x} with pattern select */
__global__ void fraction_to_the_boundry_w_pattern_hip(int n,
double* yd,
const double* xd,
const double* dd,
const double* id,
double tau)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(dd[i] >= 0 || id[i] == 0) {
yd[i] = 1.0;
} else {
yd[i] = -tau * xd[i] / dd[i];
}
}
}
/** @brief y[i] = 0 if id[i]==0.0 && xd[i]!=0.0, otherwise y[i] = 1*/
__global__ void set_match_pattern_hip(int n, int* yd, const double* xd, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(id[i] == 0.0 && xd[i] != 0.0) {
yd[i] = 0;
} else {
yd[i] = 1;
}
}
}
/** @brief Adjusts duals. */
__global__ void adjust_duals_hip(int n, double* zd, const double* xd, const double* id, double mu, double kappa)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
double a, b;
for(int i = tid; i < n; i += num_threads) {
// preemptive loop to reduce number of iterations?
if(id[i] == 1.) {
// precompute a and b in another loop?
a = mu / xd[i];
b = a / kappa;
a = a * kappa;
// Necessary conditionals
if(zd[i] < b) {
zd[i] = b;
} else {
// zd[i]>=b
if(a <= b) {
zd[i] = b;
} else {
// a>b
if(a < zd[i]) {
zd[i] = a;
}
}
}
// - - - -
// else a>=z[i] then *z=*z (z[i] does not need adjustment)
}
}
}
/// set nonlinear type
__global__ void set_nonlinear_type_hip(const int n,
const int length,
hiop::hiopInterfaceBase::NonlinearityType* arr,
const int start,
const hiop::hiopInterfaceBase::NonlinearityType* arr_src,
const int start_src)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n && i < length; i += num_threads) {
arr[start + i] = arr_src[start_src + i];
}
}
/// set nonlinear type
__global__ void set_nonlinear_type_hip(const int n,
const int length,
hiop::hiopInterfaceBase::NonlinearityType* arr,
const int start,
const hiop::hiopInterfaceBase::NonlinearityType arr_src)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n && i < length; i += num_threads) {
arr[start + i] = arr_src;
}
}
/// for hiopVectorIntHip
/**
* @brief Set the vector entries to be a linear space of starting at i0 containing evenly
* incremented integers up to i0+(n-1)di, when n is the length of this vector
*/
__global__ void set_to_linspace_hip(int n, int* vec, int i0, int di)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
vec[i] = i0 + i * di;
}
}
/** @brief compute cusum from the given pattern*/
__global__ void compute_cusum_hip(int n, int* vec, const double* id)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid; i < n; i += num_threads) {
if(i == 0) {
vec[i] = 0;
} else {
// from i=1..n
if(id[i - 1] != 0.0) {
vec[i] = 1;
} else {
vec[i] = 0;
}
}
}
}
/// @brief Copy the entries in 'dd' where corresponding 'ix' is nonzero, to vd starting at start_index_in_dest.
__global__ void copyToStartingAt_w_pattern_hip(int n_src,
int n_dest,
int start_index_in_dest,
int* nnz_cumsum,
double* vd,
const double* dd)
{
const int num_threads = blockDim.x * gridDim.x;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
for(int i = tid + 1; i < n_src + 1; i += num_threads) {
if(nnz_cumsum[i] != nnz_cumsum[i - 1]) {
int idx_dest = nnz_cumsum[i - 1] + start_index_in_dest;
vd[idx_dest] = dd[i - 1];
}
}
}
namespace hiop
{
namespace hip
{
constexpr int block_size = 256;
/// @brief Copy from src the elements specified by the indices in id.
void copy_from_index_kernel(int n_local, double* yd, const double* src, const int* id)
{
int num_blocks = (n_local + block_size - 1) / block_size;
copy_from_index_hip<<<num_blocks, block_size>>>(n_local, yd, src, id);
}
/** @brief Set y[i] = min(y[i],c), for i=[0,n_local-1] */
void component_min_kernel(int n_local, double* yd, double c)
{
int num_blocks = (n_local + block_size - 1) / block_size;
component_min_hip<<<num_blocks, block_size>>>(n_local, yd, c);
}
/** @brief Set y[i] = min(y[i],x[i], for i=[0,n_local-1] */
void component_min_kernel(int n_local, double* yd, const double* xd)
{
int num_blocks = (n_local + block_size - 1) / block_size;
component_min_hip<<<num_blocks, block_size>>>(n_local, yd, xd);
}
/** @brief Set y[i] = max(y[i],c), for i=[0,n_local-1] */
void component_max_kernel(int n_local, double* yd, double c)
{
int num_blocks = (n_local + block_size - 1) / block_size;
component_max_hip<<<num_blocks, block_size>>>(n_local, yd, c);
}
/** @brief Set y[i] = max(y[i],x[i]), for i=[0,n_local-1] */
void component_max_kernel(int n_local, double* yd, const double* xd)
{
int num_blocks = (n_local + block_size - 1) / block_size;
component_max_hip<<<num_blocks, block_size>>>(n_local, yd, xd);
}
/// @brief Performs axpy, y += alpha*x, on the indexes in this specified by id.
void axpy_w_map_kernel(int n_local, double* yd, const double* xd, const int* id, double alpha)
{
int num_blocks = (n_local + block_size - 1) / block_size;
axpy_w_map_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id, alpha);
}
/** @brief y[i] += alpha*x[i]*z[i] forall i */
void axzpy_kernel(int n_local, double* yd, const double* xd, const double* zd, double alpha)
{
int num_blocks = (n_local + block_size - 1) / block_size;
axzpy_hip<<<num_blocks, block_size>>>(n_local, yd, xd, zd, alpha);
}
/** @brief y[i] += alpha*x[i]/z[i] forall i */
void axdzpy_kernel(int n_local, double* yd, const double* xd, const double* zd, double alpha)
{
int num_blocks = (n_local + block_size - 1) / block_size;
axdzpy_hip<<<num_blocks, block_size>>>(n_local, yd, xd, zd, alpha);
}
/** @brief y[i] += alpha*x[i]/z[i] forall i with pattern selection */
void axdzpy_w_pattern_kernel(int n_local, double* yd, const double* xd, const double* zd, const double* id, double alpha)
{
int num_blocks = (n_local + block_size - 1) / block_size;
axdzpy_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, zd, id, alpha);
}
/** @brief y[i] += c forall i */
void add_constant_kernel(int n_local, double* yd, double c)
{
int num_blocks = (n_local + block_size - 1) / block_size;
add_constant_hip<<<num_blocks, block_size>>>(n_local, yd, c);
}
/** @brief y[i] += c forall i with pattern selection */
void add_constant_w_pattern_kernel(int n_local, double* yd, const double* id, double c)
{
int num_blocks = (n_local + block_size - 1) / block_size;
add_constant_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, c, id);
}
/// @brief Invert (1/x) the elements of this
void invert_kernel(int n_local, double* yd)
{
int num_blocks = (n_local + block_size - 1) / block_size;
invert_hip<<<num_blocks, block_size>>>(n_local, yd);
}
/** @brief y[i] += alpha*1/x[i] + y[i] forall i with pattern selection */
void adxpy_w_pattern_kernel(int n_local, double* yd, const double* xd, const double* id, double alpha)
{
int num_blocks = (n_local + block_size - 1) / block_size;
adxpy_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id, alpha);
}
/** @brief elements of this that corespond to nonzeros in ix are divided by elements of v.
The rest of elements of this are set to zero.*/
void component_div_w_pattern_kernel(int n_local, double* yd, const double* xd, const double* id)
{
int num_blocks = (n_local + block_size - 1) / block_size;
component_div_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id);
}
/** @brief Linear damping term */
void set_linear_damping_term_kernel(int n_local, double* yd, const double* vd, const double* ld, const double* rd)
{
// compute linear damping term
int num_blocks = (n_local + block_size - 1) / block_size;
set_linear_damping_term_hip<<<num_blocks, block_size>>>(n_local, yd, vd, ld, rd);
}
/**
* @brief Performs `this[i] = alpha*this[i] + sign*ct` where sign=1 when EXACTLY one of
* ixleft[i] and ixright[i] is 1.0 and sign=0 otherwise.
*/
void add_linear_damping_term_kernel(int n_local, double* yd, const double* ixl, const double* ixr, double alpha, double ct)
{
int num_blocks = (n_local + block_size - 1) / block_size;
add_linear_damping_term_hip<<<num_blocks, block_size>>>(n_local, yd, ixl, ixr, alpha, ct);
}
/** @brief Checks if selected elements of `this` are positive */
void is_posive_w_pattern_kernel(int n_local, double* yd, const double* xd, const double* id)
{
int num_blocks = (n_local + block_size - 1) / block_size;
is_posive_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id);
}
/// set value with pattern
void set_val_w_pattern_kernel(int n_local, double* yd, const double* xd, const double* id, double max_val)
{
int num_blocks = (n_local + block_size - 1) / block_size;
set_val_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id, max_val);
}
/** @brief Project solution into bounds */
void project_into_bounds_kernel(int n_local,
double* xd,
const double* xld,
const double* ild,
const double* xud,
const double* iud,
double kappa1,
double kappa2,
double small_real)
{
int num_blocks = (n_local + block_size - 1) / block_size;
project_into_bounds_hip<<<num_blocks, block_size>>>(n_local, xd, xld, ild, xud, iud, kappa1, kappa2, small_real);
}
/** @brief max{a\in(0,1]| x+ad >=(1-tau)x} */
void fraction_to_the_boundry_kernel(int n_local, double* yd, const double* xd, const double* dd, double tau)
{
int num_blocks = (n_local + block_size - 1) / block_size;
fraction_to_the_boundry_hip<<<num_blocks, block_size>>>(n_local, yd, xd, dd, tau);
}
/** @brief max{a\in(0,1]| x+ad >=(1-tau)x} with pattern select */
void fraction_to_the_boundry_w_pattern_kernel(int n_local,
double* yd,
const double* xd,
const double* dd,
const double* id,
double tau)
{
int num_blocks = (n_local + block_size - 1) / block_size;
fraction_to_the_boundry_w_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, dd, id, tau);
}
/** @brief Set elements of `this` to zero based on `select`.*/
void select_pattern_kernel(int n_local, double* yd, const double* id)
{
int num_blocks = (n_local + block_size - 1) / block_size;
select_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, id);
}
/** @brief y[i] = 0 if id[i]==0.0 && xd[i]!=0.0, otherwise y[i] = 1*/
void component_match_pattern_kernel(int n_local, int* yd, const double* xd, const double* id)
{
int num_blocks = (n_local + block_size - 1) / block_size;
set_match_pattern_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id);
}
/** @brief Adjusts duals. */
void adjustDuals_plh_kernel(int n_local, double* yd, const double* xd, const double* id, double mu, double kappa)
{
int num_blocks = (n_local + block_size - 1) / block_size;
adjust_duals_hip<<<num_blocks, block_size>>>(n_local, yd, xd, id, mu, kappa);
}
/// @brief set int array 'arr', starting at `start` and ending at `end`, to the values in `arr_src` from 'start_src`
void set_array_from_to_kernel(int n_local,
hiop::hiopInterfaceBase::NonlinearityType* arr,
int start,
int length,
const hiop::hiopInterfaceBase::NonlinearityType* arr_src,
int start_src)
{
int num_blocks = (n_local + block_size - 1) / block_size;
set_nonlinear_type_hip<<<num_blocks, block_size>>>(n_local, length, arr, start, arr_src, start_src);
}
/// @brief set int array 'arr', starting at `start` and ending at `end`, to the values in `arr_src` from 'start_src`
void set_array_from_to_kernel(int n_local,
hiop::hiopInterfaceBase::NonlinearityType* arr,
int start,
int length,
hiop::hiopInterfaceBase::NonlinearityType arr_src)
{
int num_blocks = (n_local + block_size - 1) / block_size;
set_nonlinear_type_hip<<<num_blocks, block_size>>>(n_local, length, arr, start, arr_src);
}
/// @brief Set all elements to c.
void thrust_fill_kernel(int n, double* ptr, double c)
{
thrust::device_ptr<double> dev_ptr = thrust::device_pointer_cast(ptr);
thrust::fill(thrust::device, dev_ptr, dev_ptr + n, c);
}
/** @brief inf norm on single rank */
double infnorm_local_kernel(int n, double* data_dev)
{
thrust_abs<double> abs_op;
thrust::maximum<double> max_op;
thrust::device_ptr<double> dev_ptr = thrust::device_pointer_cast(data_dev);
// compute one norm
double norm = thrust::transform_reduce(thrust::device, data_dev, data_dev + n, abs_op, 0.0, max_op);
return norm;
}
/** @brief Return the one norm */
double onenorm_local_kernel(int n, double* data_dev)
{
thrust_abs<double> abs_op;
thrust::plus<double> plus_op;
thrust::device_ptr<double> dev_ptr = thrust::device_pointer_cast(data_dev);
// thrust::device_ptr<double> dev_ptr(data_dev);
// compute one norm
double norm = thrust::transform_reduce(thrust::device, data_dev, data_dev + n, abs_op, 0.0, plus_op);
return norm;
}
/** @brief d1[i] = d1[i] * d2[i] forall i */
void thrust_component_mult_kernel(int n, double* d1, const double* d2)
{
// wrap raw pointer with a device_ptr
thrust::multiplies<double> mult_op;
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
thrust::device_ptr<const double> dev_v2 = thrust::device_pointer_cast(d2);
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v2, dev_v1, mult_op);
}
/** @brief d1[i] = d1[i] / d2[i] forall i */
void thrust_component_div_kernel(int n, double* d1, const double* d2)
{
// wrap raw pointer with a device_ptr
thrust::divides<double> div_op;
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
thrust::device_ptr<const double> dev_v2 = thrust::device_pointer_cast(d2);
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v2, dev_v1, div_op);
}
/** @brief d1[i] = abs(d1[i]) forall i */
void thrust_component_abs_kernel(int n, double* d1)
{
// wrap raw pointer with a device_ptr
thrust_abs<double> abs_op;
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
// compute abs
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v1, abs_op);
}
/** @brief d1[i] = sign(d1[i]) forall i */
void thrust_component_sgn_kernel(int n, double* d1)
{
// wrap raw pointer with a device_ptr
thrust_sig<double> sig_op;
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
// compute sign
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v1, sig_op);
}
/** @brief d1[i] = sqrt(d1[i]) forall i */
void thrust_component_sqrt_kernel(int n, double* d1)
{
// wrap raw pointer with a device_ptr
thrust_sqrt<double> sqrt_op;
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
// compute sqrt
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v1, sqrt_op);
}
/** @brief d1[i] = -(d1[i]) forall i */
void thrust_negate_kernel(int n, double* d1)
{
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
thrust::transform(thrust::device, dev_v1, dev_v1 + n, dev_v1, thrust::negate<double>());
}
/** @brief compute sum(log(d1[i])) forall i where id[i]=1*/
double log_barr_obj_kernel(int n, double* d1, const double* id)
{
thrust::device_ptr<double> dev_v = thrust::device_pointer_cast(d1);
thrust::device_ptr<const double> id_v = thrust::device_pointer_cast(id);
// wrap raw pointer with a device_ptr
thrust_log_select<double> log_select_op;
thrust::plus<double> plus_op;
thrust::multiplies<double> mult_op;
// TODO: how to avoid this temp vec?
thrust::device_ptr<double> v_temp = thrust::device_malloc(n * sizeof(double));
// compute x*id
thrust::transform(thrust::device, dev_v, dev_v + n, id_v, v_temp, mult_op);
// compute log(y) for y > 0
double sum = thrust::transform_reduce(thrust::device, v_temp, v_temp + n, log_select_op, 0.0, plus_op);
thrust::device_free(v_temp);
return sum;
}
/** @brief compute sum(w[i]*log(d1[i])) forall i where id[i]=1*/
double log_barr_wei_obj_kernel(int n, double* d1, const double* id, const double* w)
{
thrust::device_ptr<double> dev_v = thrust::device_pointer_cast(d1);
thrust::device_ptr<const double> id_v = thrust::device_pointer_cast(id);
thrust::device_ptr<const double> wei_v = thrust::device_pointer_cast(w);
// wrap raw pointer with a device_ptr
thrust_log_select<double> log_select_op;
thrust::plus<double> plus_op;
thrust::multiplies<double> mult_op;
// TODO: how to avoid this temp vec?
thrust::device_ptr<double> v_temp = thrust::device_malloc(n*sizeof(double));
// compute v=x*id
thrust::transform(thrust::device, dev_v, dev_v+n, id_v, v_temp, mult_op);
// compute v[i] = ( log(v[i]) if v[i]>0, otherwise 0 )
thrust::transform(thrust::device, v_temp, v_temp+n, v_temp, log_select_op);
// compute v[i] = w[i]*v[i]
thrust::transform(thrust::device, v_temp, v_temp+n, wei_v, v_temp, mult_op);
// sum up
const double sum = thrust::reduce(thrust::device, v_temp, v_temp+n, 0.0, plus_op);
thrust::device_free(v_temp);
return sum;
}
/** @brief compute sum(d1[i]) */
double thrust_sum_kernel(int n, double* d1)
{
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
// compute sum
return thrust::reduce(thrust::device, dev_v1, dev_v1 + n, 0.0, thrust::plus<double>());
}
/** @brief Linear damping term */
double linear_damping_term_kernel(int n, const double* vd, const double* ld, const double* rd, double mu, double kappa_d)
{
// TODO: how to avoid this temp vec?
thrust::device_vector<double> v_temp(n);
double* dv_ptr = thrust::raw_pointer_cast(v_temp.data());
// compute linear damping term
hiop::hip::set_linear_damping_term_kernel(n, dv_ptr, vd, ld, rd);
double term = thrust::reduce(thrust::device, v_temp.begin(), v_temp.end(), 0.0, thrust::plus<double>());
term *= mu;
term *= kappa_d;
return term;
}
/** @brief compute min(d1) */
double min_local_kernel(int n, double* d1)
{
thrust::device_ptr<double> dev_v1 = thrust::device_pointer_cast(d1);
thrust::device_ptr<double> ret_dev_ptr = thrust::min_element(thrust::device, dev_v1, dev_v1 + n);