Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
mma.cu
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 <cuda_runtime.h>
43#include <cusolverDn.h>
44
45// Neko includes
46#include <neko/device/device_config.h>
47#include <neko/device/cuda/check.h>
48#include <neko/math/bcknd/device/device_mpi_reduce.h>
49#include <neko/math/bcknd/device/device_mpi_op.h>
50
51// Local includes
52#include "mma_kernel.h"
53
54
55extern "C" {
56
57 int mma_red_s = 0;
58 real* mma_bufred = NULL;
59 real* mma_bufred_d = NULL;
60
61 void mma_update_hessian_z_cuda(void* Hess, void* a, int* m) {
62 const int M = *m;
63 // This function is called ONLY if dot(lambda,a) > 0
64
65 const int total = M * M;
66 const dim3 nthrds(1024, 1, 1);
67 const dim3 nblcks((total + 1024 - 1) / 1024, 1, 1);
68
70 (real*)Hess, (real*)a, M);
71
73 }
74
75 void mma_prepare_aa_matrix_cuda(void* AA, void* s, void* lambda,
76 void* d, void* mu, void* y,
77 void* a, real* zeta, real* z, int* m) {
78 const dim3 nthrds(1024, 1, 1);
79 const dim3 nblcks(((*m) + 1024 - 1) / 1024, 1, 1);
80
81 // Launch kernel to prepare AA matrix
83 (real*)AA, (real*)s, (real*)lambda, (real*)d,
84 (real*)mu, (real*)y, (real*)a,
85 *zeta, *z, *m);
86
88 }
89
90 void mma_prepare_hessian_cuda(void* Hess, void* y,
91 void* mu, void* lambda, int* m) {
92 const int M = *m;
93 const dim3 nthrds(1024, 1, 1);
94 const dim3 nblcks((M + 1024 - 1) / 1024, 1, 1);
96
97 // Update diagonal elements
99 (real*)Hess, (real*)y, (real*)mu, (real*)lambda, M);
101
102 // Synchronize to ensure diagonal updates are complete
104
105 // Choose kernel based on problem size
106 if (M <= 1024) {
107 // Single-block version (fast for small m)
108 const dim3 stab_nblcks(1, 1, 1);
110 (real*)Hess, M);
112 } else {
113 // Multi-block version (for large m)
114 // Compute trace on host (simple and reliable)
115 real* h_Hess = (real*)malloc(M * sizeof(real));
116
117 // Extract diagonal elements
118 for (int i = 0; i < M; i++) {
120 (real*)Hess + i * M + i,
121 sizeof(real),
123 }
125
126 // Compute trace and LM factor
127 real trace = 0.0;
128 for (int i = 0; i < M; i++) {
129 trace += h_Hess[i];
130 }
131 real lm_factor = fmax(-1.0e-4 * trace / M, 1.0e-7);
132
133 // Apply stabilization in parallel
135 (real*)Hess, lm_factor, M);
137
138 free(h_Hess);
139 }
140 }
141
142 void cuda_custom_solver(void* A, void* b, int n, int* info) {
144
145 if (n <= 0) {
146 *info = -1; // Use CPU fallback
147 return;
148 }
149 const dim3 nthrds(1024, 1, 1);
150 const dim3 nblcks(1, 1, 1);
151
153 (real*)A, (real*)b, n);
154
156 if (err == cudaSuccess) {
157 *info = 0; // GPU solver succeeded
158 } else {
159 *info = -1; // GPU failed
160 }
161 }
162
163 void cuSOLVER_wrapper(void* A, void* b, int n, int* jj) {
168 *jj = -1;
169 return;
170 }
171
172 // Solve on Neko's stream, ordered after the kernels assembling A and b.
173 // The stream is non-blocking, so the info values are also copied on it.
174 // A failure is returned through jj, which the caller reports.
178 *jj = -1;
179 return;
180 }
181
182 int lwork;
183 double* workspace;
184 int* ipiv;
185 int* info; // Device pointer for cuSOLVER info
186 int host_info = 0; // Host variable to store the info
187
188 // Workspace query
189 status = cusolverDnDgetrf_bufferSize(handle, n, n, (double*)A, n, &lwork);
192 }
193 cudaMalloc(&workspace, lwork * sizeof(double));
194 cudaMalloc(&ipiv, n * sizeof(int));
195 cudaMalloc(&info, sizeof(int));
196
197 // LU factorization and solve
198 cusolverDnDgetrf(handle, n, n, (double*)A, n, workspace, ipiv, info);
199
200 // Copy info from device to host to check if factorization succeeded
204
205 if (host_info == 0) {
206 // Only solve if factorization was successful
207 cusolverDnDgetrs(handle, CUBLAS_OP_N, n, 1, (double*)A, n, ipiv, (double*)b, n, info);
208 // Copy the final info value
212 }
213
214
215 // Return the actual info value through jj
216 *jj = host_info;
217
218 // Cleanup
220 cudaFree(ipiv);
221 cudaFree(info);
223 }
224
225 void custom_solve_linear_system(void* A, void* b, int n, int* info) {
227
228 if (n <= 0) {
229 *info = -1; // Use CPU fallback
230 return;
231 }
232 const dim3 nthrds(1024, 1, 1);
233 const dim3 nblcks(1, 1, 1);
234
236 (real*)A, (real*)b, n);
237
239 if (err == cudaSuccess) {
240 *info = 0; // GPU solver succeeded
241 } else {
242 *info = -1; // GPU failed, use CPU fallback
243 }
244 }
245
246 void delta_1dbeam_cuda(void* Delta, real* L_total, real* Le,
247 int* offset, int* n) {
248 const dim3 nthrds(1024, 1, 1);
249 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
251 ((real*)Delta, *L_total, *Le, *offset, *n);
253 }
254
255 void cuda_Hess(void* Hess, void* hijx, void* Ljjxinv, int *n, int *m) {
256 const dim3 nthrds(1024, 1, 1);
257 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
258 const int nb = ((*n) + 1024 - 1)/ 1024;
261 if(nb > mma_red_s){
262 mma_red_s = nb;
263 if(mma_bufred != NULL){
264 CUDA_CHECK(cudaFreeHost(mma_bufred));
265 CUDA_CHECK(cudaFree(mma_bufred_d));
266 }
267 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
268 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
269 }
270 for (int i = 0; i < (*m); i++){
271 for (int j=0; j<(*m);j++){
273 ((real*)hijx,(real*)Ljjxinv, mma_bufred_d, (*n),(*m), i, j);
275 mmareduce_kernel<real> <<<1, 1024, 0, stream>>> (mma_bufred_d, nb);
277 mma_copy_kernel<<<1, 1, 0, stream>>>((real*)Hess, mma_bufred_d, 1,
278 i+j*(*m));
281 }
282 }
283 }
284
285 void mma_Ljjxinv_cuda(void* Ljjxinv, void* pjlambda, void* qjlambda, void* x,
286 void* low, void* upp, void* alpha, void* beta, int* n) {
287 const dim3 nthrds(1024, 1, 1);
288 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
290 ((real*)Ljjxinv, (real*)pjlambda, (real*)qjlambda, (real*)x, (real*)low,
291 (real*)upp, (real*)alpha, (real*)beta, *n);
293 }
294
295 void mma_dipsolvesub1_cuda(void* x, void* pjlambda, void* qjlambda,
296 void* low, void* upp, void* alpha, void* beta, int* n) {
297 const dim3 nthrds(1024, 1, 1);
298 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
300 ((real*)x, (real*)pjlambda, (real*)qjlambda, (real*)low, (real*)upp,
301 (real*)alpha, (real*)beta, *n);
303 }
304
305 void mattrans_v_mul_cuda(void* output, void* pij, void* lambda,
306 int* m, int* n) {
307 const dim3 nthrds(1024, 1, 1);
308 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
310 ((real*)output, (real*)pij, (real*)lambda, *m, *n);
312 }
313
314 void mma_gensub4_cuda(const void* x, const void* low, const void* upp,
315 const void* pij, const void* qij,
316 const int* n, const int* m, void* bi) {
317
318 const int N = *n;
319 const int M = *m;
320
321 const dim3 nthrds(1024, 1, 1);
322 const dim3 nblcks((N + 1023) / 1024, 1, 1);
323 const int nb = (N + 1023) / 1024;
325
326 if (nb > mma_red_s) {
327 mma_red_s = nb;
328
329 if (mma_bufred != NULL) {
330 CUDA_CHECK(cudaFreeHost(mma_bufred));
331 CUDA_CHECK(cudaFree(mma_bufred_d));
332 }
333
334 CUDA_CHECK(cudaMallocHost(&mma_bufred, nb * sizeof(real)));
335 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb * sizeof(real)));
336 }
337
338 real* temp;
339 real* bi_d = static_cast<real*>(bi);
340 CUDA_CHECK(cudaMalloc(&temp, M * N * sizeof(real)));
341
343 static_cast<const real*>(x),
344 static_cast<const real*>(low),
345 static_cast<const real*>(upp),
346 static_cast<const real*>(pij),
347 static_cast<const real*>(qij),
348 temp, N, M);
349
350 for (int i = 0; i < M; ++i) {
352 temp, mma_bufred_d, N, M, i);
354
355 mmareduce_kernel<real><<<1, 1024, 0, stream>>>(mma_bufred_d, nb);
357
359 bi_d + i, mma_bufred_d, sizeof(real),
361
363 }
364
365 CUDA_CHECK(cudaFree(temp));
366 }
367
368 void mma_gensub3_cuda(void* x, void* df0dx, void* dfdx, void* low,
369 void* upp, void* xmin, void* xmax, void* alpha, void* beta,
370 void* p0j, void* q0j, void* pij, void* qij, int* n, int* m) {
371 const dim3 nthrds(1024, 1, 1);
372 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
373
376 (real*)x, (real*)df0dx, (real*)dfdx, (real*)low,
377 (real*)upp, (real*)xmin, (real*)xmax, (real*)alpha,
378 (real*)beta, (real*)p0j, (real*)q0j, (real*)pij,
379 (real*)qij, *n, *m);
380
382 }
383
384 void mma_gensub3_dip_cuda(void* x, void* df0dx, void* dfdx, void* low,
385 void* upp, void* xmin, void* xmax, void* alpha, void* beta,
386 void* p0j, void* q0j, void* pij, void* qij, int* n, int* m) {
387 const dim3 nthrds(1024, 1, 1);
388 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
389
392 (real*)x, (real*)df0dx, (real*)dfdx, (real*)low,
393 (real*)upp, (real*)xmin, (real*)xmax, (real*)alpha,
394 (real*)beta, (real*)p0j, (real*)q0j, (real*)pij,
395 (real*)qij, *n, *m);
396
398 }
399
400 void mma_gensub2_cuda(void* low, void* upp, void* x, void* xold1,
401 void* xold2, void* xdiff, real* asydecr, real* asyincr, int* n) {
402 const dim3 nthrds(1024, 1, 1);
403 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
404
407 (real*)low, (real*)upp, (real*)x, (real*)xold1,
408 (real*)xold2, (real*)xdiff, *asydecr, *asyincr, *n);
409
411 }
412
413 void mma_gensub1_cuda(void* low, void* upp, void* x, void* xmin, void* xmax,
414 real* asyinit, int* n) {
415 const dim3 nthrds(1024, 1, 1);
416 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
418 ((real*)low, (real*)upp, (real*)x, (real*)xmin, (real*)xmax,
419 *asyinit, *n);
421 }
422
423 void cuda_mma_max(void* xsi, void* x, void* alpha, int* n) {
424 const dim3 nthrds(1024, 1, 1);
425 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
426
428 ((real*)xsi, (real*)x, (real*)alpha, *n);
430 }
431
432 void cuda_relambda(void* relambda, void* x, void* xupp, void* xlow,
433 void* pij, void* qij, int* n, int* m) {
434 const dim3 nthrds(1024, 1, 1);
435 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
436 const int nb = ((*n) + 1024 - 1)/ 1024;
438
439 if ( nb > mma_red_s){
440 mma_red_s = nb;
441 if (mma_bufred != NULL) {
442 CUDA_CHECK(cudaFreeHost(mma_bufred));
443 CUDA_CHECK(cudaFree(mma_bufred_d));
444 }
445 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
446 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
447 }
448 real* temp;
449 cudaMalloc(&temp, (*n) * (*m) * sizeof(real));
450 relambda_kernel<real> <<<nblcks, nthrds, 0, stream >>> (temp, (real*)x,
451 (real*)xupp, (real*)xlow, (real*)pij, (real*)qij, *n, *m);
452 for (int i = 0; i < (*m); i++) {
454 (temp, mma_bufred_d, (*n),(*m), i);
456 mmareduce_kernel<real> <<<1, 1024, 0, stream >>>
457 (mma_bufred_d, nb);
459 mma_copy_kernel<<<1, 1, 0, stream>>>((real*)relambda, mma_bufred_d, 1, i);
462 }
463 cudaFree(temp);
464 }
465
466 void cuda_sub2cons2(void* a, void* b, void* c, void* d, real* e, int* n) {
467
468 const dim3 nthrds(1024, 1, 1);
469 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
470
472 (real*)a, (real*)b, (real*)c, (real*)d, *e, *n);
474 }
475
476 //max abs values of input
477 real cuda_maxval(void* a, int* n) {
478
479 const dim3 nthrds(1024, 1, 1);
480 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
481 const int nb = ((*n) + 1024 - 1) / 1024;
483
484 if (nb > mma_red_s) {
485 mma_red_s = nb;
486 if (mma_bufred != NULL) {
487 CUDA_CHECK(cudaFreeHost(mma_bufred));
488 CUDA_CHECK(cudaFree(mma_bufred_d));
489 }
490 CUDA_CHECK(cudaMallocHost(&mma_bufred, nb * sizeof(real)));
491 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb * sizeof(real)));
492 }
493
495 (real*)a, mma_bufred_d, (*n));
497
498 max_reduce_kernel<real><<<1, 1024, 0, stream>>>(
499 mma_bufred_d, nb);
501
502 CUDA_CHECK(cudaMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
505
506 return mma_bufred[0];
507 }
508
509 void cuda_delx(void* delx, void* x, void* xlow, void* xupp, void* pij,
510 void* qij, void* p0j, void* q0j, void* alpha, void* beta, void* lambda,
511 real* epsi, int* n, int* m) {
512
513 const dim3 nthrds(1024, 1, 1);
514 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
515
517 (real*)delx, (real*)x, (real*)xlow, (real*)xupp, (real*)pij,
518 (real*)qij, (real*)p0j, (real*)q0j, (real*)alpha, (real*)beta,
519 (real*)lambda, *epsi, *n, *m);
521 }
522
523 void cuda_GG(void* GG, void* x, void* xlow, void* xupp,
524 void* pij, void* qij, int* n, int* m) {
525 const dim3 nthrds(1024, 1, 1);
526 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
528 ((real*)GG, (real*)x, (real*)xlow,(real*) xupp,
529 (real*)pij, (real*) qij, *n,*m);
531 }
532
533 void cuda_diagx(void* diagx, void* x, void* xsi,void* xlow, void* xupp,
534 void* p0j, void* q0j, void* pij, void* qij, void* alpha, void* beta,
535 void* eta, void* lambda, int *n, int *m) {
536 const dim3 nthrds(1024, 1, 1);
537 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
539 ((real*)diagx, (real*)x, (real*)xsi,(real*)xlow,
540 (real*) xupp,(real*)p0j, (real*) q0j, (real*)pij, (real*) qij,
541 (real*)alpha, (real*) beta, (real*)eta, (real*) lambda, *n,*m);
543 }
544
545 void cuda_bb(void* bb, void* GG, void* delx,void* diagx, int *n, int *m) {
546 const dim3 nthrds(1024, 1, 1);
547 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
548 const int nb = ((*n) + 1024 - 1)/ 1024;
551 if(nb > mma_red_s){
552 mma_red_s = nb;
553 if(mma_bufred != NULL){
554 CUDA_CHECK(cudaFreeHost(mma_bufred));
555 CUDA_CHECK(cudaFree(mma_bufred_d));
556 }
557 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
558 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
559 }
560 for (int i = 0; i < (*m); i++) {
562 ((real*)GG,(real*)delx,(real*)diagx, mma_bufred_d, (*n),(*m), i);
564 mmareduce_kernel<real> <<<1, 1024, 0, stream>>> (mma_bufred_d, nb);
566 mma_copy_kernel<<<1, 1, 0, stream>>>((real*)bb, mma_bufred_d, 1, i);
569 }
570 }
571
572 void cuda_AA(void* AA, void* GG, void* diagx, int *n, int *m) {
573 const dim3 nthrds(1024, 1, 1);
574 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
575 const int nb = ((*n) + 1024 - 1)/ 1024;
578 if(nb > mma_red_s){
579 mma_red_s = nb;
580 if(mma_bufred != NULL){
581 CUDA_CHECK(cudaFreeHost(mma_bufred));
582 CUDA_CHECK(cudaFree(mma_bufred_d));
583 }
584 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
585 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
586 }
587 for (int i = 0; i < (*m); i++){
588 for (int j=0; j<(*m);j++){
590 ((real*)GG,(real*)diagx, mma_bufred_d, (*n),(*m), i, j);
592 mmareduce_kernel<real> <<<1, 1024, 0, stream>>> (mma_bufred_d, nb);
594 mma_copy_kernel<<<1, 1, 0, stream>>>((real*)AA, mma_bufred_d, 1,
595 i+j*(*m+1));
598 }
599 }
600 }
601
602 void cuda_dx(void* dx,void* delx, void* diagx, void* GG, void* dlambda,
603 int* n, int* m) {
604 const dim3 nthrds(1024, 1, 1);
605 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
607 ((real*)dx, (real*)delx, (real*)diagx,(real*) GG, (real*)dlambda,
608 *n,*m);
610 }
611
612 void cuda_dxsi(void* dxsi, void* xsi, void* dx,void* x,
613 void* alpha, real*epsi, int* n) {
614 const dim3 nthrds(1024, 1, 1);
615 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
617 ((real*)dxsi, (real*)xsi, (real*)dx,(real*) x,
618 (real*)alpha, *epsi,*n);
620 }
621
622 void cuda_deta(void* deta, void* eta, void* dx, void* x,
623 void* beta, real* epsi, int* n) {
624 const dim3 nthrds(1024, 1, 1);
625 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
627 ((real*)deta, (real*)eta, (real*)dx, (real*)x,
628 (real*)beta, *epsi, *n);
630 }
631
632 void cuda_rex(void* rex, void* x, void* xlow, void* xupp, void* pij,
633 void* p0j, void* qij, void* q0j, void* lambda, void* xsi, void* eta,
634 int* n, int* m) {
635 const dim3 nthrds(1024, 1, 1);
636 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
639 (real*)xupp, (real*)pij, (real*)p0j, (real*)qij, (real*)q0j,
640 (real*)lambda, (real*)xsi, (real*)eta, *n, *m);
642 }
643
644 void cuda_rey(void* rey, void* c, void* d, void* y, void* lambda, void* mu,
645 int* n) {
646 const dim3 nthrds(1024, 1, 1);
647 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
650 (real*)d, (real*)y, (real*)lambda, (real*)mu, * n);
652 }
653
654
655 void cuda_sub2cons(void * a,void * b,void * c, real *d, int * n) {
656 const dim3 nthrds(1024, 1, 1);
657 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
659 ((real *) a, (real *) b, (real *) c, *d, *n);
661 }
662
663 real cuda_norm(void* a, int* n) {
664 const dim3 nthrds(1024, 1, 1);
665 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
666 const int nb = ((*n) + 1024 - 1)/ 1024;
668 if(nb > mma_red_s){
669 mma_red_s = nb;
670 if(mma_bufred != NULL){
671 CUDA_CHECK(cudaFreeHost(mma_bufred));
672 CUDA_CHECK(cudaFree(mma_bufred_d));
673 }
674 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
675 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
676 }
678 ((real*)a, mma_bufred_d, (*n));
680 mmareduce_kernel<real> <<<1, 1024, 0, stream >>> (mma_bufred_d, nb);
682 CUDA_CHECK(cudaMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
685 return mma_bufred[0];
686 }
687
688 void cuda_dely(void* dely, void* c, void* d, void* y, void* lambda,
689 real* epsi, int* n) {
690 const dim3 nthrds(1024, 1, 1);
691 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
693 ((real*)dely,(real*)c, (real*)d, (real*)y, (real*)lambda,*epsi, * n);
695 }
696
697 real cuda_maxval2(void* a, void* b, real* cons, int* n) {
698 const dim3 nthrds(1024, 1, 1);
699 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
700 const int nb = ((*n) + 1024 - 1)/ 1024;
702 if(nb > mma_red_s){
703 mma_red_s = nb;
704 if(mma_bufred != NULL) {
705 CUDA_CHECK(cudaFreeHost(mma_bufred));
706 CUDA_CHECK(cudaFree(mma_bufred_d));
707 }
708 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
709 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
710 }
712 ((real*)a, (real*)b, mma_bufred_d, *cons, *n);
714 max_reduce_kernel<real> <<<1, 1024, 0,stream >>> (mma_bufred_d, nb);
716 CUDA_CHECK(cudaMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
719 return mma_bufred[0];
720 }
721
722 real cuda_maxval3(void* a, void* b, void* c, real* cons, int* n) {
723 const dim3 nthrds(1024, 1, 1);
724 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
725 const int nb = ((*n) + 1024 - 1)/ 1024;
727 if(nb > mma_red_s){
728 mma_red_s = nb;
729 if(mma_bufred != NULL) {
730 CUDA_CHECK(cudaFreeHost(mma_bufred));
731 CUDA_CHECK(cudaFree(mma_bufred_d));
732 }
733 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
734 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
735 }
737 ((real*)a, (real*)b, (real*)c, mma_bufred_d, *cons, *n);
738 max_reduce_kernel<real> <<<1, 1024, 0,stream >>> (mma_bufred_d, nb);
740 CUDA_CHECK(cudaMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
743 return mma_bufred[0];
744 }
745
746 void cuda_kkt_rex(void* rex, void* df0dx, void* dfdx, void* xsi,
747 void* eta, void* lambda, int* n, int* m) {
748 const dim3 nthrds(1024, 1, 1);
749 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
751 ((real*)rex, (real*)df0dx, (real*)dfdx, (real*)xsi,
752 (real*)eta, (real*)lambda, *n, *m);
754 }
755
756 void mma_dip_kkt_cuda(void* res, void* x, void* df0dx, void* dfdx,
757 void* xmin, void* xmax, void* lambda, int* n, int* m) {
758 if (*n < 1) return;
759
760 const dim3 nthrds(1024, 1, 1);
761 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
764 (real*)res, (real*)x, (real*)df0dx, (real*)dfdx, (real*)xmin,
765 (real*)xmax, (real*)lambda, *n, *m);
767 }
768
769 void cuda_maxcons(void* a, real* b, real* c, void* d, int* n) {
770 const dim3 nthrds(1024, 1, 1);
771 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
773 ((real*)a, *b, *c, (real*)d, *n);
775 }
776
777 real cuda_lcsc2(void *a, void*b, int *n) {
778 const dim3 nthrds(1024, 1, 1);
779 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
780 const int nb = ((*n) + 1024 - 1)/ 1024;
782 if ( nb > mma_red_s){
783 mma_red_s = nb;
784 if (mma_bufred != NULL) {
785 CUDA_CHECK(cudaFreeHost(mma_bufred));
786 CUDA_CHECK(cudaFree(mma_bufred_d));
787 }
788 CUDA_CHECK(cudaMallocHost(&mma_bufred,nb*sizeof(real)));
789 CUDA_CHECK(cudaMalloc(&mma_bufred_d, nb*sizeof(real)));
790 }
792 ((real*)a, (real*)b, mma_bufred_d, (*n));
794 mmareduce_kernel<real> <<<1, 1024, 0, stream>>> (mma_bufred_d, nb);
796 CUDA_CHECK(cudaMemcpyAsync(mma_bufred, mma_bufred_d, sizeof(real),
799 return mma_bufred[0];
800 }
801
802 void cuda_mpisum(void *a, int *n) {
803#ifdef HAVE_DEVICE_MPI
804 real* temp=(real*)a;
807#endif
808 }
809
810 void cuda_add2inv2(void* a, void *b, real* c, int* n) {
811 const dim3 nthrds(1024, 1, 1);
812 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
814 ((real*)a, (real*) b, *c, *n);
816 }
817
818 void cuda_max2(void* a, real* b, void* c, real* d, int* n) {
819 const dim3 nthrds(1024, 1, 1);
820 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
822 ((real*)a, *b, (real*)c,*d, *n);
824 }
825
826 void cuda_updatebb(void* bb, void* dellambda, void* dely,void* d,
827 void* mu, void* y, real* delz, int *m) {
828 const dim3 nthrds(1024, 1, 1);
829 const dim3 nblcks(((*m+1) + 1024 - 1) / 1024, 1, 1);
831 ((real*) bb, (real*) dellambda, (real*) dely,(real*) d,
832 (real*) mu, (real*) y, *delz, *m);
834 }
835
836 void cuda_updateAA(void* AA, void* globaltmp_mm, void* s, void* lambda,
837 void* d, void*mu,void* y,void* a, real* zeta, real* z, int* m) {
838 const dim3 nthrds(1024, 1, 1);
839 const dim3 nblcks(((*m+1) + 1024 - 1) / 1024, 1, 1);
841 ((real*) AA,(real*) globaltmp_mm, (real*) s, (real*) lambda,(real*) d,
842 (real*)mu,(real*) y, (real*)a, *zeta, *z, *m);
844 }
845
846 void cuda_dy(void* dy, void* dely, void* dlambda,void* d, void* mu,
847 void* y, int* n) {
848 const dim3 nthrds(1024, 1, 1);
849 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
851 ((real*) dy, (real*) dely, (real*) dlambda, (real*) d,
852 (real*) mu,(real*) y, *n);
854 }
855
856}/* extern "C" */
__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)