Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
mma.hip
Go to the documentation of this file.
1
37// System includes
38#include <stdio.h>
39#include <stdlib.h>
40
41// Device includes
42#include <hip/hip_runtime.h>
43#include <hipsolver/hipsolver.h>
44
45// Neko includes
46#include <neko/device/device_config.h>
47#include <neko/device/hip/check.h>
48#include <neko/math/bcknd/device/device_mpi_op.h>
49
50// Local includes
51#include "mma_kernel.h"
52
53extern "C" {
54
55int mma_red_s = 0;
56real * mma_bufred = NULL;
57real * mma_bufred_d = NULL;
58
59void mma_update_hessian_z_hip(void* Hess, void* a, int m) {
60 const int M = m;
61 const int total = M * M;
62
63 const dim3 nthrds(1024, 1, 1);
64 const dim3 nblcks((total + 1024 - 1) / 1024, 1, 1);
65 const hipStream_t stream = (hipStream_t)glb_cmd_queue; // same as CUDA stream
66
69 (real*)Hess, (real*)a, M);
71}
72
73void hipSOLVER_wrapper(void* A, void* b, int n, int* jj) {
78 *jj = -1;
79 return;
80 }
81
82 // Solve on Neko's stream, ordered after the kernels assembling A and b.
83 // The stream is non-blocking, so the info values are also copied on it.
84 // A failure is returned through jj, which the caller reports.
88 *jj = -1;
89 return;
90 }
91
92 int lwork;
93 double *workspace;
94 int *ipiv;
95 int *info; // Device pointer for hipSOLVER info
96 int host_info = 0; // Host variable to store the info
97
98 // Workspace query
99 status = hipsolverDnDgetrf_bufferSize(handle, n, n, (double*)A, n, &lwork);
100 HIP_CHECK(hipMalloc(&workspace, lwork * sizeof(double)));
101 HIP_CHECK(hipMalloc(&ipiv, n * sizeof(int)));
102 HIP_CHECK(hipMalloc(&info, sizeof(int)));
103
104 // LU factorization and solve
105 hipsolverDnDgetrf(handle, n, n, (double*)A, n, workspace, ipiv, info);
106
107 // Copy info from device to host to check if factorization succeeded
108 HIP_CHECK(hipMemcpyAsync(&host_info, info, sizeof(int),
111
112 if (host_info == 0) {
113 // Only solve if factorization was successful
114 hipsolverDnDgetrs(handle, HIPSOLVER_OP_N, n, 1, (double*)A, n, ipiv, (double*)b, n, info);
115 // Copy the final info value
116 HIP_CHECK(hipMemcpyAsync(&host_info, info, sizeof(int),
119 }
120
121 // Return the actual info value through jj
122 *jj = host_info;
123
124 // Cleanup
129}
130
131void mma_prepare_aa_matrix_hip(void* AA, void* s, void* lambda,
132 void* d, void* mu, void* y,
133 void* a, real zeta, real z, int m) {
134 const int M = m;
135 const int matrix_size = M + 1;
136 const dim3 nthrds(256, 1, 1);
137 const dim3 nblcks((M + 256 - 1) / 256, 1, 1);
139
140 // Launch kernel to prepare AA matrix entirely on device
142 nblcks, nthrds, 0, stream,
143 (real*)AA, (real*)s, (real*)lambda, (real*)d,
144 (real*)mu, (real*)y, (real*)a, zeta, z, M);
145
147}
148
149void mma_prepare_hessian_hip(void* Hess, void* y,
150 void* mu, void* lambda, int m) {
151 const int M = m;
152 const dim3 nthrds(1024, 1, 1);
153 const dim3 nblcks((M + 1024 - 1) / 1024, 1, 1);
155
156 // Update diagonal elements
158 nblcks, nthrds, 0, stream,
159 (real*)Hess, (real*)y, (real*)mu, (real*)lambda, M);
161
162 // Synchronize to ensure diagonal updates are complete
164
165 // Choose kernel based on problem size
166 if (M <= 1024) {
167 // Single-block version (fast for small m)
168 const dim3 stab_nblcks(1, 1, 1);
171 (real*)Hess, M);
173 } else {
174 // Multi-block version (for large m)
175 // Compute trace on host (simple and reliable)
176 real* h_Hess = (real*)malloc(M * sizeof(real));
177
178 // Extract diagonal elements
179 for (int i = 0; i < M; i++) {
181 (real*)Hess + i * M + i,
182 sizeof(real),
184 }
186
187 // Compute trace and LM factor
188 real trace = 0.0;
189 for (int i = 0; i < M; i++) {
190 trace += h_Hess[i];
191 }
192 real lm_factor = fmax(-1.0e-4 * trace / M, 1.0e-7);
193
194 // Apply stabilization in parallel
196 nblcks, nthrds, 0, stream,
197 (real*)Hess, lm_factor, M);
199
200 free(h_Hess);
201 }
202}
203
204// Custom linear solver using kernel
205extern "C" void hip_custom_solver(void* A, void* b, int n, int* info) {
207
208 if (n <= 0) {
209 *info = -1; // Use CPU fallback
210 return;
211 }
212 const dim3 nthrds(1024, 1, 1);
213 const dim3 nblcks(1, 1, 1);
214
216 (real*)A, (real*)b, n);
217
219 if (err == hipSuccess) {
220 *info = 0; // GPU solver succeeded
221 } else {
222 *info = -1; // GPU failed
223 }
224}
225
226void delta_1dbeam_hip(void* Delta, real* L_total, real* Le,
227 int* offset, int* n) {
228 const dim3 nthrds(1024, 1, 1);
229 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
232 (real*)Delta, *L_total, *Le, *offset, *n);
234}
235
236void hip_Hess(void* Hess, void* hijx, void* Ljjxinv, int *n, int *m) {
237 const dim3 nthrds(1024, 1, 1);
238 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
239 const int nb = ((*n) + 1024 - 1) / 1024;
242
243 if (nb > mma_red_s) {
244 mma_red_s = nb;
245 if (mma_bufred != NULL) {
246 HIP_CHECK(hipHostFree(mma_bufred));
247 HIP_CHECK(hipFree(mma_bufred_d));
248 }
249 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
250 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
251 }
252
253 for (int i = 0; i < (*m); i++) {
254 for (int j = 0; j < (*m); j++) {
256 (real*)hijx, (real*)Ljjxinv, mma_bufred_d, (*n), (*m), i, j);
258
260 mma_bufred_d, nb);
262
263 hipLaunchKernelGGL(mma_copy_kernel, dim3(1), dim3(1), 0, stream,
264 (real*)Hess, mma_bufred_d, 1, i + j * (*m));
266
268 }
269 }
270}
271
272void mma_Ljjxinv_hip(void* Ljjxinv, void* pjlambda, void* qjlambda, void* x,
273 void* low, void* upp, void* alpha, void* beta, int* n) {
274 const dim3 nthrds(1024, 1, 1);
275 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
278 (real*)x, (real*)low, (real*)upp, (real*)alpha, (real*)beta, *n);
280}
281
282void mma_dipsolvesub1_hip(void* x, void* pjlambda, void* qjlambda, void* low,
283 void* upp, void* alpha, void* beta, int* n) {
284 const dim3 nthrds(1024, 1, 1);
285 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
288 (real*)low, (real*)upp, (real*)alpha, (real*)beta, *n);
290}
291
292void mattrans_v_mul_hip(void* output, void* pij, void* lambda, int* m, int* n) {
293 const dim3 nthrds(1024, 1, 1);
294 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
296 (hipStream_t)glb_cmd_queue, (real*)output, (real*)pij, (real*)lambda, *m, *n);
298}
299
300void mma_gensub4_hip(const void* x, const void* low, const void* upp,
301 const void* pij, const void* qij,
302 const int* n, const int* m, void* bi) {
303
304 const int N = *n;
305 const int M = *m;
306
307 const dim3 nthrds(1024, 1, 1);
308 const dim3 nblcks((N + 1023) / 1024, 1, 1);
309 const int nb = (N + 1023) / 1024;
311
312 if (nb > mma_red_s) {
313 mma_red_s = nb;
314
315 if (mma_bufred != nullptr) {
316 HIP_CHECK(hipFreeHost(mma_bufred));
317 HIP_CHECK(hipFree(mma_bufred_d));
318 }
319
320 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
321 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
322 }
323
324 real* temp;
325 real* bi_d = static_cast<real*>(bi);
326 HIP_CHECK(hipMalloc(&temp, M * N * sizeof(real)));
327
329 static_cast<const real*>(x),
330 static_cast<const real*>(low),
331 static_cast<const real*>(upp),
332 static_cast<const real*>(pij),
333 static_cast<const real*>(qij),
334 temp, N, M);
335
336 for (int i = 0; i < M; ++i) {
338 temp, mma_bufred_d, N, M, i);
340
342 mma_bufred_d, nb);
344
346 bi_d + i, mma_bufred_d, sizeof(real),
348
350 }
351
352 HIP_CHECK(hipFree(temp));
353}
354
355
356void mma_gensub3_hip(void* x, void* df0dx, void* dfdx, void* low,
357 void* upp, void* xmin, void* xmax, void* alpha,
358 void* beta, void* p0j, void* q0j, void* pij,
359 void* qij, int* n, int* m) {
360 const dim3 nthrds(1024, 1, 1);
361 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
362
365 (real*)x, (real*)df0dx, (real*)dfdx, (real*)low,
366 (real*)upp, (real*)xmin, (real*)xmax, (real*)alpha,
367 (real*)beta, (real*)p0j, (real*)q0j, (real*)pij,
368 (real*)qij, *n, *m);
369
371}
372
373void mma_gensub3_dip_hip(void* x, void* df0dx, void* dfdx, void* low,
374 void* upp, void* xmin, void* xmax, void* alpha,
375 void* beta, void* p0j, void* q0j, void* pij,
376 void* qij, int* n, int* m) {
377 const dim3 nthrds(1024, 1, 1);
378 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
379
382 (real*)x, (real*)df0dx, (real*)dfdx, (real*)low,
383 (real*)upp, (real*)xmin, (real*)xmax, (real*)alpha,
384 (real*)beta, (real*)p0j, (real*)q0j, (real*)pij,
385 (real*)qij, *n, *m);
386
388}
389
390void mma_gensub2_hip(void* low, void* upp, void* x, void* xold1,
391 void* xold2, void* xdiff, real* asydecr,
392 real* asyincr, int* n) {
393 const dim3 nthrds(1024, 1, 1);
394 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
395
398 (real*)low, (real*)upp, (real*)x, (real*)xold1,
399 (real*)xold2, (real*)xdiff, *asydecr, *asyincr, *n);
400
402}
403
404void mma_gensub1_hip(void* low, void* upp, void* x, void* xmin, void* xmax,
405 real* asyinit, int* n) {
406 const dim3 nthrds(1024, 1, 1);
407 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
408
411 (real*)low, (real*)upp, (real*)x, (real*)xmin, (real*)xmax,
412 *asyinit, *n);
413
415}
416
417void hip_mma_max(void* xsi, void* x, void* alpha, int* n) {
418 const dim3 nthrds(1024, 1, 1);
419 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
420
423 (real*)xsi, (real*)x, (real*)alpha, *n);
424
426}
427
428void hip_relambda(void* relambda, void* x, void* xupp, void* xlow,
429 void* pij, void* qij, int* n, int* m) {
430 const dim3 nthrds(1024, 1, 1);
431 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
432 const int nb = nblcks.x;
434
435 if (nb > mma_red_s) {
436 mma_red_s = nb;
437 if (mma_bufred != NULL) {
438 HIP_CHECK(hipHostFree(mma_bufred));
439 HIP_CHECK(hipFree(mma_bufred_d));
440 }
441 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
442 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
443 }
444
445 real* temp;
446 HIP_CHECK(hipMalloc(&temp, (*n) * (*m) * sizeof(real)));
447
449 temp, (real*)x, (real*)xupp, (real*)xlow,
450 (real*)pij, (real*)qij, *n, *m);
451
452 for (int i = 0; i < (*m); i++) {
454 temp, mma_bufred_d, (*n), (*m), i);
456
458 stream, mma_bufred_d, nb);
460
461 hipLaunchKernelGGL(mma_copy_kernel, dim3(1), dim3(1), 0, stream,
462 (real*)relambda, mma_bufred_d, 1, i);
464
466 }
467
468 HIP_CHECK(hipFree(temp));
469}
470
471void hip_sub2cons2(void* a, void* b, void* c, void* d, real* e, int* n) {
472 const dim3 nthrds(1024, 1, 1);
473 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
474
477 (real*)a, (real*)b, (real*)c, (real*)d, *e, *n);
478
480}
481
482real hip_maxval(void* a, int* n) {
483 const dim3 nthrds(1024, 1, 1);
484 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
485 const int nb = nblcks.x;
487
488 if (nb > mma_red_s) {
489 mma_red_s = nb;
490 if (mma_bufred != NULL) {
491 HIP_CHECK(hipHostFree(mma_bufred));
492 HIP_CHECK(hipFree(mma_bufred_d));
493 }
494 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
495 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
496 }
497
499 (real*)a, mma_bufred_d, (*n));
501
503 mma_bufred_d, nb);
505
506 HIP_CHECK(hipMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
509
510 return mma_bufred[0];
511}
512
513
514void hip_delx(void* delx, void* x, void* xlow, void* xupp, void* pij,
515 void* qij, void* p0j, void* q0j, void* alpha, void* beta, void* lambda,
516 real* epsi, int* n, int* m) {
517 const dim3 nthrds(1024, 1, 1);
518 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
519
522 (real*)delx, (real*)x, (real*)xlow, (real*)xupp, (real*)pij,
523 (real*)qij, (real*)p0j, (real*)q0j, (real*)alpha, (real*)beta,
524 (real*)lambda, *epsi, *n, *m);
526}
527
528void hip_GG(void* GG, void* x, void* xlow, void* xupp,
529 void* pij, void* qij, int* n, int* m) {
530 const dim3 nthrds(1024, 1, 1);
531 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
532
535 (real*)GG, (real*)x, (real*)xlow, (real*)xupp, (real*)pij,
536 (real*)qij, *n, *m);
538}
539
540void hip_diagx(void* diagx, void* x, void* xsi, void* xlow, void* xupp,
541 void* p0j, void* q0j, void* pij, void* qij, void* alpha, void* beta,
542 void* eta, void* lambda, int *n, int *m) {
543 const dim3 nthrds(1024, 1, 1);
544 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
545
548 (real*)diagx, (real*)x, (real*)xsi, (real*)xlow, (real*)xupp,
549 (real*)p0j, (real*)q0j, (real*)pij, (real*)qij, (real*)alpha,
550 (real*)beta, (real*)eta, (real*)lambda, *n, *m);
552}
553
554void hip_bb(void* bb, void* GG, void* delx, void* diagx, int *n, int *m) {
555 const dim3 nthrds(1024, 1, 1);
556 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
557 const int nb = ((*n) + 1024 - 1)/ 1024;
559
561
562 if (nb > mma_red_s) {
563 mma_red_s = nb;
564 if (mma_bufred != NULL) {
565 HIP_CHECK(hipHostFree(mma_bufred));
566 HIP_CHECK(hipFree(mma_bufred_d));
567 }
568 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
569 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
570 }
571
572 for (int i = 0; i < (*m); i++) {
574 (real*)GG, (real*)delx, (real*)diagx, mma_bufred_d, *n, *m, i);
576
578 mma_bufred_d, nb);
580
581 hipLaunchKernelGGL(mma_copy_kernel, 1, 1, 0, stream, (real*)bb,
582 mma_bufred_d, 1, i);
584
586 }
587}
588
589void hip_AA(void* AA, void* GG, void* diagx, int *n, int *m) {
590 const dim3 nthrds(1024, 1, 1);
591 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
592 const int nb = ((*n) + 1024 - 1)/ 1024;
594
596
597 if (nb > mma_red_s) {
598 mma_red_s = nb;
599 if (mma_bufred != NULL) {
600 HIP_CHECK(hipHostFree(mma_bufred));
601 HIP_CHECK(hipFree(mma_bufred_d));
602 }
603 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
604 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
605 }
606
607 for (int i = 0; i < (*m); i++) {
608 for (int j = 0; j < (*m); j++) {
610 (real*)GG, (real*)diagx, mma_bufred_d, *n, *m, i, j);
612
614 mma_bufred_d, nb);
616
617 hipLaunchKernelGGL(mma_copy_kernel, 1, 1, 0, stream,
618 (real*)AA, mma_bufred_d, 1, i + j * (*m + 1));
620
622 }
623 }
624}
625
626void hip_dx(void* dx, void* delx, void* diagx, void* GG, void* dlambda,
627 int* n, int* m) {
628 const dim3 nthrds(1024, 1, 1);
629 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
630
633 (real*)dx, (real*)delx, (real*)diagx, (real*)GG, (real*)dlambda, *n, *m);
635}
636
637void hip_dxsi(void* dxsi, void* xsi, void* dx, void* x,
638 void* alpha, real* epsi, int* n) {
639 const dim3 nthrds(1024, 1, 1);
640 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
641
644 (real*)dxsi, (real*)xsi, (real*)dx, (real*)x, (real*)alpha, *epsi, *n);
646}
647
648void hip_deta(void* deta, void* eta, void* dx, void* x,
649 void* beta, real* epsi, int* n) {
650 const dim3 nthrds(1024, 1, 1);
651 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
652
655 (real*)deta, (real*)eta, (real*)dx, (real*)x, (real*)beta, *epsi, *n);
657}
658
659void hip_rex(void* rex, void* x, void* xlow, void* xupp, void* pij,
660 void* p0j, void* qij, void* q0j, void* lambda, void* xsi, void* eta,
661 int* n, int* m) {
662 const dim3 nthrds(1024, 1, 1);
663 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
664
667 (real*)rex, (real*)x, (real*)xlow, (real*)xupp, (real*)pij, (real*)p0j,
668 (real*)qij, (real*)q0j, (real*)lambda, (real*)xsi, (real*)eta, *n, *m);
670}
671
672void hip_rey(void* rey, void* c, void* d, void* y, void* lambda, void* mu, int* n) {
673 const dim3 nthrds(1024, 1, 1);
674 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
675
678 (real*)rey, (real*)c, (real*)d, (real*)y, (real*)lambda, (real*)mu, *n);
680}
681
682
683 // a_d = b_d * c_d - d
684void hip_sub2cons(void *a, void *b, void *c, real *d, int *n) {
685 const dim3 nthrds(1024, 1, 1);
686 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
689 (real *)a, (real *)b, (real *)c, *d, *n);
691}
692
693
694// sum(a^2)
695real hip_norm(void* a, int* n) {
696 const dim3 nthrds(1024, 1, 1);
697 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
698 const int nb = ((*n) + 1024 - 1) / 1024;
700
701 if (nb > mma_red_s) {
702 mma_red_s = nb;
703 if (mma_bufred != NULL) {
704 HIP_CHECK(hipFreeHost(mma_bufred));
705 HIP_CHECK(hipFree(mma_bufred_d));
706 }
707 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
708 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
709 }
710
712 (real*)a, mma_bufred_d, (*n));
714
716 mma_bufred_d, nb);
718
719 HIP_CHECK(hipMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
721
723
724 return mma_bufred[0];
725}
726
727
728void hip_dely(void* dely, void* c, void* d, void* y, void* lambda,
729 real* epsi, int* n) {
730 const dim3 nthrds(1024, 1, 1);
731 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
734 (real*)dely, (real*)c, (real*)d, (real*)y, (real*)lambda, *epsi, *n);
736}
737
738
739real hip_maxval2(void* a, void* b, real* cons, int* n) {
740 const dim3 nthrds(1024, 1, 1);
741 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
742 const int nb = ((*n) + 1024 - 1) / 1024;
744
745 if (nb > mma_red_s) {
746 mma_red_s = nb;
747 if (mma_bufred != NULL) {
748 HIP_CHECK(hipFreeHost(mma_bufred));
749 HIP_CHECK(hipFree(mma_bufred_d));
750 }
751 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
752 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
753 }
754
756 (real*)a, (real*)b, mma_bufred_d, *cons, *n);
758
760 mma_bufred_d, nb);
762
763 HIP_CHECK(hipMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
765
767
768 return mma_bufred[0];
769}
770
771
772real hip_maxval3(void* a, void* b, void* c, real* cons, int* n) {
773 const dim3 nthrds(1024, 1, 1);
774 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
775 const int nb = ((*n) + 1024 - 1) / 1024;
777
778 if (nb > mma_red_s) {
779 mma_red_s = nb;
780 if (mma_bufred != NULL) {
781 HIP_CHECK(hipFreeHost(mma_bufred));
782 HIP_CHECK(hipFree(mma_bufred_d));
783 }
784 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
785 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
786 }
787
789 (real*)a, (real*)b, (real*)c, mma_bufred_d, *cons, *n);
791 mma_bufred_d, nb);
793
794 HIP_CHECK(hipMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
796
798
799 return mma_bufred[0];
800}
801
802
803void hip_kkt_rex(void* rex, void* df0dx, void* dfdx, void* xsi,
804 void* eta, void* lambda, int* n, int* m) {
805 const dim3 nthrds(1024, 1, 1);
806 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
809 (real*)rex, (real*)df0dx, (real*)dfdx, (real*)xsi,
810 (real*)eta, (real*)lambda, *n, *m);
812}
813
814void mma_dip_kkt_hip(void* res, void* x, void* df0dx, void* dfdx,
815 void* xmin, void* xmax, void* lambda, int* n, int* m) {
816 if (*n < 1) return;
817
818 const dim3 nthrds(1024, 1, 1);
819 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
822 (real*)res, (real*)x, (real*)df0dx, (real*)dfdx, (real*)xmin,
823 (real*)xmax, (real*)lambda, *n, *m);
825}
826
827
828// a_d = max(b, c * d_d)
829void hip_maxcons(void* a, real* b, real* c, void* d, int* n) {
830 const dim3 nthrds(1024, 1, 1);
831 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
834 (real*)a, *b, *c, (real*)d, *n);
836}
837
838
839real hip_lcsc2(void *a, void*b, int *n) {
840 const dim3 nthrds(1024, 1, 1);
841 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
842 const int nb = ((*n) + 1024 - 1) / 1024;
844
845 if (nb > mma_red_s) {
846 mma_red_s = nb;
847 if (mma_bufred != NULL) {
848 HIP_CHECK(hipFreeHost(mma_bufred));
849 HIP_CHECK(hipFree(mma_bufred_d));
850 }
851 HIP_CHECK(hipHostMalloc(&mma_bufred, nb * sizeof(real)));
852 HIP_CHECK(hipMalloc(&mma_bufred_d, nb * sizeof(real)));
853 }
854
856 (real*)a, (real*)b, mma_bufred_d, (*n));
858
860 mma_bufred_d, nb);
862
863 HIP_CHECK(hipMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
865
867
868 return mma_bufred[0];
869}
870
871
872void hip_mpisum(void *a, int *n) {
873#ifdef HAVE_DEVICE_MPI
874 real* temp = (real*)a;
877#endif
878}
879
880
881void hip_add2inv2(void* a, void* b, real* c, int* n) {
882 const dim3 nthrds(1024, 1, 1);
883 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
885
887 (real*)a, (real*)b, *c, *n);
889}
890
891void hip_max2(void* a, real* b, void* c, real* d, int* n) {
892 const dim3 nthrds(1024, 1, 1);
893 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
895
897 (real*)a, *b, (real*)c, *d, *n);
899}
900
901void hip_updatebb(void* bb, void* dellambda, void* dely, void* d,
902 void* mu, void* y, real* delz, int* m) {
903 const dim3 nthrds(1024, 1, 1);
904 const dim3 nblcks(((*m + 1) + 1024 - 1) / 1024, 1, 1);
906
908 (real*)bb, (real*)dellambda, (real*)dely, (real*)d,
909 (real*)mu, (real*)y, *delz, *m);
911}
912
913void hip_updateAA(void* AA, void* globaltmp_mm, void* s, void* lambda,
914 void* d, void* mu, void* y, void* a,
915 real* zeta, real* z, int* m) {
916 const dim3 nthrds(1024, 1, 1);
917 const dim3 nblcks(((*m + 1) + 1024 - 1) / 1024, 1, 1);
919
921 (real*)AA, (real*)globaltmp_mm, (real*)s,
922 (real*)lambda, (real*)d, (real*)mu,
923 (real*)y, (real*)a, *zeta, *z, *m);
925}
926
927void hip_dy(void* dy, void* dely, void* dlambda, void* d,
928 void* mu, void* y, int* n) {
929 const dim3 nthrds(1024, 1, 1);
930 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
932
934 (real*)dy, (real*)dely, (real*)dlambda, (real*)d,
935 (real*)mu, (real*)y, *n);
937}
938}
__global__ void Borrvall_Petersson_mapping_apply_kernel(const T f_min, const T f_max, const T q, T *__restrict__ X_out_d, T *__restrict__ X_in_d, const int n)