-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathceed-cuda-gen-operator-build.cpp
More file actions
2724 lines (2410 loc) · 123 KB
/
ceed-cuda-gen-operator-build.cpp
File metadata and controls
2724 lines (2410 loc) · 123 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-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
//
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of CEED: http://github.com/ceed
#define CEED_DEBUG_COLOR 12
#include <ceed.h>
#include <ceed/backend.h>
#include <ceed/gen-tools.h>
#include <ceed/jit-tools.h>
#include <cuda_runtime.h>
#include <iostream>
#include <sstream>
#include <string>
#include "../cuda-ref/ceed-cuda-ref.h"
#include "../cuda-shared/ceed-cuda-shared.h"
#include "../cuda/ceed-cuda-common.h"
#include "../cuda/ceed-cuda-compile.h"
#include "ceed-cuda-gen.h"
struct FieldReuse_Cuda {
CeedInt index;
bool is_input;
CeedEvalMode eval_mode;
};
//------------------------------------------------------------------------------
// Determine type of operator
//------------------------------------------------------------------------------
static int CeedOperatorBuildKernelData_Cuda_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d,
CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) {
// Check if all are tensor
*is_all_tensor = true;
for (CeedInt i = 0; i < num_input_fields; i++) {
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
if (basis != CEED_BASIS_NONE) {
bool is_field_tensor;
CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
*is_all_tensor = *is_all_tensor && is_field_tensor;
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
for (CeedInt i = 0; i < num_output_fields; i++) {
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
if (basis != CEED_BASIS_NONE) {
bool is_field_tensor;
CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
*is_all_tensor = *is_all_tensor && is_field_tensor;
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
// Find max_P, max_P_1d, Q, and Q_1d
bool is_all_3d = true;
*max_P = 0;
*max_P_1d = 0;
*Q = 0;
*Q_1d = 0;
for (CeedInt i = 0; i < num_input_fields; i++) {
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
if (basis != CEED_BASIS_NONE) {
bool is_field_tensor;
CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
// Check if 3D
CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
is_all_3d = is_all_3d && (field_dim == 3);
*max_dim = CeedIntMax(*max_dim, field_dim);
// Collect P, P_1d, Q, and Q_1d
CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
*max_P = CeedIntMax(*max_P, field_P);
CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
if (is_field_tensor) {
CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
*max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
}
CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
*Q = field_Q;
if (is_field_tensor) {
CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
*Q_1d = field_Q_1d;
}
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
for (CeedInt i = 0; i < num_output_fields; i++) {
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
if (basis != CEED_BASIS_NONE) {
bool is_field_tensor;
CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
// Check if 3D
CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
is_all_3d = is_all_3d && (field_dim == 3);
*max_dim = CeedIntMax(*max_dim, field_dim);
// Collect P, P_1d, Q, and Q_1d
CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
*max_P = CeedIntMax(*max_P, field_P);
CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
if (is_field_tensor) {
CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
*max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
}
CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
*Q = field_Q;
if (is_field_tensor) {
CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
*Q_1d = field_Q_1d;
}
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
// Only use 3D collocated gradient parallelization strategy when gradient is computed
*use_3d_slices = false;
if (is_all_3d && *is_all_tensor) {
bool was_grad_found = false;
for (CeedInt i = 0; i < num_input_fields; i++) {
CeedEvalMode eval_mode;
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_GRAD) {
CeedBasis_Cuda_shared *basis_data;
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
CeedCallBackend(CeedBasisGetData(basis, &basis_data));
*use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
was_grad_found = true;
CeedCallBackend(CeedBasisDestroy(&basis));
}
}
for (CeedInt i = 0; i < num_output_fields; i++) {
CeedEvalMode eval_mode;
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_GRAD) {
CeedBasis_Cuda_shared *basis_data;
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
CeedCallBackend(CeedBasisGetData(basis, &basis_data));
*use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
was_grad_found = true;
CeedCallBackend(CeedBasisDestroy(&basis));
}
}
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Setup fields
//------------------------------------------------------------------------------
static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
CeedOperatorField op_field, CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse,
CeedInt max_dim, CeedInt Q, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
bool use_3d_slices, bool skip_active_load) {
bool is_tensor = true, is_active = true;
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
{
CeedVector vec;
CeedCallBackend(CeedOperatorFieldGetVector(op_field, &vec));
is_active = vec == CEED_VECTOR_ACTIVE;
CeedCallBackend(CeedVectorDestroy(&vec));
}
const char *field_name;
std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
std::string option_name = (is_input ? "inputs" : "outputs");
CeedEvalMode eval_mode = CEED_EVAL_NONE;
CeedInt elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0;
CeedElemRestriction elem_rstr;
CeedBasis_Cuda_shared *basis_data;
// Field reuse info
bool use_previous_field = field_reuse.index != -1;
CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
code << tab << "// -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
// Get field data
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
if (basis != CEED_BASIS_NONE) {
CeedCallBackend(CeedBasisGetData(basis, &basis_data));
CeedCallBackend(CeedBasisGetDimension(basis, &dim));
if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
}
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
// Set field constants
code << tab << "const CeedInt dim" << var_suffix << " = " << dim << ";\n";
if (is_tensor && !is_all_tensor) {
CeedInt P = 0;
CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
code << tab << "const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n";
}
code << tab << "const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
if (eval_mode != CEED_EVAL_WEIGHT) {
code << tab << "const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
}
// Load basis data
code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
switch (eval_mode) {
case CEED_EVAL_NONE:
break;
case CEED_EVAL_INTERP:
if (is_at_points) {
// AtPoints
if (!basis_data->d_chebyshev_interp_1d) {
CeedSize interp_bytes;
CeedScalar *chebyshev_interp_1d;
interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
CeedCallCuda(CeedBasisReturnCeed(basis),
cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
CeedCallBackend(CeedFree(&chebyshev_interp_1d));
}
if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
} else {
// Standard quadrature
if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
else data->B.outputs[i] = basis_data->d_interp_1d;
}
if (use_previous_field && !skip_active_load) {
std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
} else {
bool is_collocated = false;
CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
if ((is_active && skip_active_load) || (is_collocated && !is_at_points)) {
code << tab << "CeedScalar *s_B" << var_suffix << " = NULL;\n";
} else {
code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
}
}
break;
case CEED_EVAL_GRAD:
if (is_at_points) {
// AtPoints
if (!basis_data->d_chebyshev_interp_1d) {
CeedSize interp_bytes;
CeedScalar *chebyshev_interp_1d;
interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
CeedCallCuda(CeedBasisReturnCeed(basis),
cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
CeedCallBackend(CeedFree(&chebyshev_interp_1d));
}
if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
} else {
// Standard quadrature
if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
else data->B.outputs[i] = basis_data->d_interp_1d;
}
if (is_tensor) {
if (use_previous_field && !skip_active_load) {
std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
} else {
bool is_collocated = false;
CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
if ((is_active && skip_active_load) || (is_collocated && !is_at_points)) {
code << tab << "CeedScalar *s_B" << var_suffix << " = NULL;\n";
} else {
code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
}
}
}
if (is_at_points) break; // No G mat for AtPoints
if (use_3d_slices) {
if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
else data->G.outputs[i] = basis_data->d_collo_grad_1d;
if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD && !skip_active_load) {
std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
} else if (is_active && skip_active_load) {
code << tab << "CeedScalar *s_G" << var_suffix << " = NULL;\n";
} else {
code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
}
} else {
bool has_collo_grad = basis_data->d_collo_grad_1d;
if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
if (has_collo_grad) {
if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD && !skip_active_load) {
std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
} else if (is_active && skip_active_load) {
code << tab << "CeedScalar *s_G" << var_suffix << " = NULL;\n";
} else {
code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
}
} else {
if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD && !skip_active_load) {
std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
} else if (is_active && skip_active_load) {
code << tab << "CeedScalar *s_G" << var_suffix << " = NULL;\n";
} else {
code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim")
<< (is_tensor ? "" : var_suffix) << "];\n";
code << tab << "LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G."
<< option_name << "[" << i << "], s_G" << var_suffix << ");\n";
}
}
}
break;
case CEED_EVAL_WEIGHT:
break; // No action
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
CeedCallBackend(CeedBasisDestroy(&basis));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Restriction
//------------------------------------------------------------------------------
static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field,
CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
bool use_3d_slices) {
std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
std::string P_name = (is_all_tensor ? "P_1d" : "P") + var_suffix;
CeedEvalMode eval_mode = CEED_EVAL_NONE;
CeedInt elem_size = 0, num_comp = 0;
CeedSize l_size;
CeedRestrictionType rstr_type = CEED_RESTRICTION_STANDARD;
CeedElemRestriction_Cuda *rstr_data;
CeedElemRestriction elem_rstr;
// Get field data
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
}
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
// Restriction
if (is_input) {
// Input
if (field_input_buffer[i] != i) {
std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
// Restriction was already done for previous input
code << tab << "CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
} else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
// No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
code << tab << "CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
} else if (rstr_type != CEED_RESTRICTION_POINTS) {
// Otherwise we're using the scratch space
code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
}
switch (rstr_type) {
case CEED_RESTRICTION_STANDARD: {
CeedInt comp_stride;
CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
code << tab << "ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
<< P_name << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix
<< ");\n";
break;
}
case CEED_RESTRICTION_STRIDED: {
bool has_backend_strides;
CeedInt num_elem;
CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
if (!has_backend_strides) {
CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
}
code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
<< ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
code << tab << "ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
<< var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, d" << var_suffix << ", r_e"
<< var_suffix << ");\n";
break;
}
case CEED_RESTRICTION_POINTS: {
CeedInt comp_stride;
CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
break;
}
case CEED_RESTRICTION_ORIENTED:
case CEED_RESTRICTION_CURL_ORIENTED:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
} else {
// Output
switch (rstr_type) {
case CEED_RESTRICTION_STANDARD: {
CeedInt comp_stride;
CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
code << tab << "{\n";
tab.push();
code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n\n";
data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
code << tab << "WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
<< P_name << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix
<< ");\n";
tab.pop();
code << tab << "}\n";
break;
}
case CEED_RESTRICTION_STRIDED: {
bool has_backend_strides;
CeedInt num_elem;
CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
if (!has_backend_strides) {
CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
}
code << tab << "{\n";
tab.push();
code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
<< ", strides" << var_suffix << "_2 = " << strides[2] << ";\n\n";
code << tab << "WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
<< var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, r_e" << var_suffix << ", d" << var_suffix
<< ");\n";
tab.pop();
code << tab << "}\n";
break;
}
case CEED_RESTRICTION_POINTS:
data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
break;
case CEED_RESTRICTION_ORIENTED:
case CEED_RESTRICTION_CURL_ORIENTED:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Basis
//------------------------------------------------------------------------------
static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d,
bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) {
bool is_tensor = true, is_collocated = true;
CeedBasis basis;
CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
CeedEvalMode eval_mode = CEED_EVAL_NONE;
CeedInt dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0;
CeedElemRestriction elem_rstr;
// Get field data
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
if (basis != CEED_BASIS_NONE) {
CeedCallBackend(CeedBasisGetDimension(basis, &dim));
if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
}
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
// Basis
code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
if (is_input) {
switch (eval_mode) {
case CEED_EVAL_NONE:
if (!use_3d_slices && !is_at_points) {
code << tab << "CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
}
break;
case CEED_EVAL_INTERP:
if (is_at_points) {
std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
<< ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
} else {
std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
: "InterpNonTensor";
std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
<< var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
}
break;
case CEED_EVAL_GRAD:
if (is_at_points) {
std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
<< ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
} else if (use_3d_slices) {
std::string function_name =
(dim > 1 ? "InterpTensor" : "Interp") + std::string(is_collocated ? "CollocatedNodes" : "") + std::to_string(dim) + "d";
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
<< ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
} else if (is_tensor) {
bool is_collocated_grad = dim == 3 && Q_1d >= P_1d;
std::string function_name =
(dim == 1 ? "Grad" : ("GradTensor" + std::string(is_collocated ? "CollocatedNodes" : (is_collocated_grad ? "Collocated" : "")))) +
std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
<< (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
<< var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
} else {
std::string function_name = "GradNonTensor";
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
<< ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
}
break;
case CEED_EVAL_WEIGHT: {
if (is_at_points) {
code << tab << "// Nothing to do AtPoints\n";
} else {
CeedBasis_Cuda_shared *basis_data;
std::string function_name = is_tensor
? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
: "WeightNonTensor";
code << tab << "CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
CeedCallBackend(CeedBasisGetData(basis, &basis_data));
data->W = basis_data->d_q_weight_1d;
code << tab << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
}
break;
}
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
} else {
switch (eval_mode) {
case CEED_EVAL_NONE:
code << tab << "CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
break; // No action
case CEED_EVAL_INTERP:
code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
if (is_at_points) {
std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
<< ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
} else {
std::string function_name =
is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
: "InterpTransposeNonTensor";
std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
<< var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
}
break;
case CEED_EVAL_GRAD:
code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
if (is_at_points) {
std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
<< ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
} else if (use_3d_slices) {
std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
std::to_string(dim) + "d";
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
<< ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
} else if (is_tensor) {
bool is_collocated_grad = dim == 3 && Q_1d >= P_1d;
std::string function_name =
(dim == 1 ? "GradTranspose"
: ("GradTransposeTensor" + std::string(is_collocated ? "CollocatedNodes" : (is_collocated_grad ? "Collocated" : "")))) +
std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
<< var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
} else {
std::string function_name = "GradTransposeNonTensor";
code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
<< ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
}
break;
// LCOV_EXCL_START
case CEED_EVAL_WEIGHT:
break; // Should not occur
// LCOV_EXCL_STOP
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
CeedCallBackend(CeedBasisDestroy(&basis));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// QFunction
//------------------------------------------------------------------------------
static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt max_dim,
CeedInt max_num_points, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
CeedQFunctionField *qf_input_fields, CeedInt num_output_fields,
CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields,
std::string qfunction_name, CeedInt Q_1d, bool is_all_tensor, bool is_at_points,
bool use_3d_slices, bool is_assemble) {
std::string Q_name = is_all_tensor ? "Q_1d" : "Q";
CeedEvalMode eval_mode = CEED_EVAL_NONE;
CeedElemRestriction elem_rstr;
// Setup output arrays
code << "\n";
code << tab << "// -- Output field setup\n";
for (CeedInt i = 0; i < num_output_fields; i++) {
const char *field_name;
std::string var_suffix = "_out_" + std::to_string(i);
CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
switch (eval_mode) {
case CEED_EVAL_NONE:
if (is_at_points) {
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
} else {
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
<< "];\n";
}
break;
case CEED_EVAL_INTERP:
if (is_at_points) {
// Accumulator for point data
code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
<< "[i] = 0.0;\n";
} else {
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
<< "];\n";
}
break;
case CEED_EVAL_GRAD:
if (is_at_points) {
// Accumulator for point data
code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
<< "[i] = 0.0;\n";
} else if (use_3d_slices) {
// Accumulator for gradient slices
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) r_q" << var_suffix << "[i] = 0.0;\n";
} else {
code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
<< (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n";
}
break;
case CEED_EVAL_WEIGHT:
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
if (is_at_points) {
// We need to handle batches of points
code << "\n";
code << tab << "// Note: Using batches of points\n";
code << tab << "const CeedInt point_loop_bound = (blockDim.x*blockDim.y) * ceil((1.0*max_num_points) / (blockDim.x*blockDim.y));\n\n";
code << tab << "#pragma unroll\n";
code << tab << "for (CeedInt i = threadIdx.x + threadIdx.y*blockDim.x; i < point_loop_bound; i += blockDim.x*blockDim.y) {\n";
tab.push();
code << tab << "const CeedInt p = i % max_num_points;\n\n";
code << tab << "// -- Coordinates\n";
code << tab << "CeedScalar r_x[max_dim];\n";
code << tab << "ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n";
code << tab << "// -- Input fields\n";
for (CeedInt i = 0; i < num_input_fields; i++) {
const char *field_name;
std::string var_suffix = "_in_" + std::to_string(i);
std::string P_name = "P_1d" + var_suffix;
CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
// Basis action
code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
switch (eval_mode) {
case CEED_EVAL_NONE:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
code << tab << "ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
<< ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
break;
case CEED_EVAL_INTERP:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
code << tab << "InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
<< ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
break;
case CEED_EVAL_GRAD:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
code << tab << "GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
<< ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
break;
case CEED_EVAL_WEIGHT:
code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
code << tab << "r_s" << var_suffix << "[0] = 1.0;\n";
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
code << "\n";
code << tab << "// -- Output fields\n";
for (CeedInt i = 0; i < num_output_fields; i++) {
const char *field_name;
std::string var_suffix = "_out_" + std::to_string(i);
CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
// Basis action
switch (eval_mode) {
case CEED_EVAL_NONE:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
break;
case CEED_EVAL_INTERP:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
break;
case CEED_EVAL_GRAD:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
break;
// LCOV_EXCL_START
case CEED_EVAL_WEIGHT:
break; // Should not occur
// LCOV_EXCL_STOP
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
} else if (use_3d_slices) {
// We treat quadrature points per slice in 3d to save registers
code << "\n";
code << tab << "// Note: Using planes of 3D elements\n";
code << tab << "#pragma unroll\n";
code << tab << "for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
tab.push();
code << tab << "// -- Input fields\n";
for (CeedInt i = 0; i < num_input_fields; i++) {
const char *field_name;
std::string var_suffix = "_in_" + std::to_string(i);
CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
// Basis action
code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
switch (eval_mode) {
case CEED_EVAL_NONE:
bool is_strided;
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
if (is_strided) {
bool has_backend_strides;
CeedInt num_elem, elem_size;
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
if (!has_backend_strides) {
CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
}
code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
<< ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
code << tab << "ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", strides" << var_suffix << "_0, strides"
<< var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
} else {
CeedSize l_size = 0;
CeedInt comp_stride;
CeedElemRestriction_Cuda *rstr_data;
CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
code << tab << "ReadEVecSliceStandard3d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", " << Q_name << ">(data, l_size"
<< var_suffix << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
break;
case CEED_EVAL_INTERP:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
tab.push();
code << tab << "r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
tab.pop();
code << tab << "}\n";
break;
case CEED_EVAL_GRAD:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
code << tab << "GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G"
<< var_suffix << ", r_s" << var_suffix << ");\n";
break;
case CEED_EVAL_WEIGHT:
code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
code << tab << "r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
code << "\n";
code << tab << "// -- Output fields\n";
for (CeedInt i = 0; i < num_output_fields; i++) {
const char *field_name;
std::string var_suffix = "_out_" + std::to_string(i);
CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
// Basis action
switch (eval_mode) {
case CEED_EVAL_NONE:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
break;
case CEED_EVAL_INTERP:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
break;
case CEED_EVAL_GRAD:
code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
break;
// LCOV_EXCL_START
case CEED_EVAL_WEIGHT:
break; // Should not occur
// LCOV_EXCL_STOP
case CEED_EVAL_DIV:
case CEED_EVAL_CURL:
data->use_fallback = true;
break; // TODO: Not implemented
}
}
} else {
code << "\n";
code << tab << "// Note: Using full elements\n";
code << tab << "{\n";
tab.push();
code << tab << "// -- Input fields\n";
for (CeedInt i = 0; i < num_input_fields; i++) {
const char *field_name;
CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
code << tab << "CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
}
code << tab << "// -- Output fields\n";
for (CeedInt i = 0; i < num_output_fields; i++) {
const char *field_name;
CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
code << tab << "CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
}
}
// Input and output buffers
code << "\n";
code << tab << "// -- QFunction inputs and outputs\n";
code << tab << "// ---- Inputs\n";
code << tab << "CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
for (CeedInt i = 0; i < num_input_fields; i++) {
const char *field_name;
CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
code << tab << "// ------ Input field " << i << ": " << field_name << "\n";
code << tab << "inputs[" << i << "] = r_s_in_" << i << ";\n";
}
code << tab << "// ---- Outputs\n";
code << tab << "CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
for (CeedInt i = 0; i < num_output_fields; i++) {
const char *field_name;