/
cuda_matrix_free.templates.h
1401 lines (1155 loc) · 48.2 KB
/
cuda_matrix_free.templates.h
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) 2016 - 2021 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_cuda_matrix_free_templates_h
#define dealii_cuda_matrix_free_templates_h
#include <deal.II/base/config.h>
#include <deal.II/matrix_free/cuda_matrix_free.h>
#ifdef DEAL_II_COMPILER_CUDA_AWARE
# include <deal.II/base/cuda_size.h>
# include <deal.II/base/graph_coloring.h>
# include <deal.II/dofs/dof_tools.h>
# include <deal.II/fe/fe_dgq.h>
# include <deal.II/fe/fe_values.h>
# include <deal.II/matrix_free/shape_info.h>
# include <cuda_runtime_api.h>
# include <cmath>
# include <functional>
DEAL_II_NAMESPACE_OPEN
namespace CUDAWrappers
{
namespace internal
{
constexpr unsigned int data_array_size =
(mf_max_elem_degree + 1) * (mf_max_elem_degree + 1);
// Default initialized to false
extern std::array<std::atomic_bool, mf_n_concurrent_objects> used_objects;
template <typename NumberType>
using DataArray = NumberType[data_array_size];
// These variables are stored in the device constant memory.
// Shape values
__constant__ double global_shape_values_d[mf_n_concurrent_objects]
[data_array_size];
__constant__ float global_shape_values_f[mf_n_concurrent_objects]
[data_array_size];
// Shape gradients
__constant__ double global_shape_gradients_d[mf_n_concurrent_objects]
[data_array_size];
__constant__ float global_shape_gradients_f[mf_n_concurrent_objects]
[data_array_size];
// for collocation methods
__constant__ double global_co_shape_gradients_d[mf_n_concurrent_objects]
[data_array_size];
__constant__ float global_co_shape_gradients_f[mf_n_concurrent_objects]
[data_array_size];
template <typename Number>
__host__ __device__ inline DataArray<Number> &
get_global_shape_values(unsigned int i);
template <>
__host__ __device__ inline DataArray<double> &
get_global_shape_values<double>(unsigned int i)
{
return global_shape_values_d[i];
}
template <>
__host__ __device__ inline DataArray<float> &
get_global_shape_values<float>(unsigned int i)
{
return global_shape_values_f[i];
}
template <typename Number>
__host__ __device__ inline DataArray<Number> &
get_global_shape_gradients(unsigned int i);
template <>
__host__ __device__ inline DataArray<double> &
get_global_shape_gradients<double>(unsigned int i)
{
return global_shape_gradients_d[i];
}
template <>
__host__ __device__ inline DataArray<float> &
get_global_shape_gradients<float>(unsigned int i)
{
return global_shape_gradients_f[i];
}
// for collocation methods
template <typename Number>
__host__ __device__ inline DataArray<Number> &
get_global_co_shape_gradients(unsigned int i);
template <>
__host__ __device__ inline DataArray<double> &
get_global_co_shape_gradients<double>(unsigned int i)
{
return global_co_shape_gradients_d[i];
}
template <>
__host__ __device__ inline DataArray<float> &
get_global_co_shape_gradients<float>(unsigned int i)
{
return global_co_shape_gradients_f[i];
}
template <typename Number>
using CUDAVector = ::dealii::LinearAlgebra::CUDAWrappers::Vector<Number>;
/**
* Transpose a N x M matrix stored in a one-dimensional array to a M x N
* matrix stored in a one-dimensional array.
*/
template <typename Number>
void
transpose(const unsigned int N,
const unsigned M,
const Number * src,
Number * dst)
{
// src is N X M
// dst is M X N
for (unsigned int i = 0; i < N; ++i)
for (unsigned int j = 0; j < M; ++j)
dst[j * N + i] = src[i * M + j];
}
/**
* Same as above but the source and the destination are the same vector.
*/
template <typename Number>
void
transpose_in_place(std::vector<Number> &array_host,
const unsigned int n,
const unsigned int m)
{
// convert to structure-of-array
std::vector<Number> old(array_host.size());
old.swap(array_host);
transpose(n, m, old.data(), array_host.data());
}
/**
* Allocate an array to the device and copy @p array_host to the device.
*/
template <typename Number1>
void
alloc_and_copy(Number1 **array_device,
const ArrayView<const Number1, MemorySpace::Host> array_host,
const unsigned int n)
{
cudaError_t error_code = cudaMalloc(array_device, n * sizeof(Number1));
AssertCuda(error_code);
AssertDimension(array_host.size(), n);
error_code = cudaMemcpy(*array_device,
array_host.data(),
n * sizeof(Number1),
cudaMemcpyHostToDevice);
AssertCuda(error_code);
}
/**
* Helper class to (re)initialize MatrixFree object.
*/
template <int dim, typename Number>
class ReinitHelper
{
public:
ReinitHelper(
MatrixFree<dim, Number> * data,
const Mapping<dim> & mapping,
const FiniteElement<dim, dim> &fe,
const Quadrature<1> & quad,
const ::dealii::internal::MatrixFreeFunctions::ShapeInfo<Number>
& shape_info,
const DoFHandler<dim> &dof_handler,
const UpdateFlags & update_flags);
void
setup_color_arrays(const unsigned int n_colors);
void
setup_cell_arrays(const unsigned int color);
template <typename CellFilter>
void
get_cell_data(
const CellFilter & cell,
const unsigned int cell_id,
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner);
void
alloc_and_copy_arrays(const unsigned int cell);
private:
MatrixFree<dim, Number> *data;
// Host data
std::vector<types::global_dof_index> local_to_global_host;
std::vector<Point<dim, Number>> q_points_host;
std::vector<Number> JxW_host;
std::vector<Number> inv_jacobian_host;
std::vector<dealii::internal::MatrixFreeFunctions::ConstraintTypes>
constraint_mask_host;
// Local buffer
std::vector<types::global_dof_index> local_dof_indices;
FEValues<dim> fe_values;
// Convert the default dof numbering to a lexicographic one
const std::vector<unsigned int> & lexicographic_inv;
std::vector<types::global_dof_index> lexicographic_dof_indices;
const unsigned int fe_degree;
const unsigned int dofs_per_cell;
const unsigned int q_points_per_cell;
const UpdateFlags & update_flags;
const unsigned int padding_length;
dealii::internal::MatrixFreeFunctions::HangingNodes<dim> hanging_nodes;
};
template <int dim, typename Number>
ReinitHelper<dim, Number>::ReinitHelper(
MatrixFree<dim, Number> * data,
const Mapping<dim> & mapping,
const FiniteElement<dim> &fe,
const Quadrature<1> & quad,
const ::dealii::internal::MatrixFreeFunctions::ShapeInfo<Number>
& shape_info,
const DoFHandler<dim> &dof_handler,
const UpdateFlags & update_flags)
: data(data)
, fe_degree(data->fe_degree)
, dofs_per_cell(data->dofs_per_cell)
, q_points_per_cell(data->q_points_per_cell)
, fe_values(mapping,
fe,
Quadrature<dim>(quad),
update_inverse_jacobians | update_quadrature_points |
update_values | update_gradients | update_JxW_values)
, lexicographic_inv(shape_info.lexicographic_numbering)
, update_flags(update_flags)
, padding_length(data->get_padding_length())
, hanging_nodes(dof_handler.get_triangulation())
{
cudaError_t error_code = cudaMemcpyToSymbol(
constraint_weights,
shape_info.data.front().subface_interpolation_matrix.data(),
sizeof(double) * fe.n_dofs_per_face(0) * fe.n_dofs_per_face(0));
local_dof_indices.resize(data->dofs_per_cell);
lexicographic_dof_indices.resize(dofs_per_cell);
}
template <int dim, typename Number>
void
ReinitHelper<dim, Number>::setup_color_arrays(const unsigned int n_colors)
{
// We need at least three colors when we are using CUDA-aware MPI and
// overlapping the communication
data->n_cells.resize(std::max(n_colors, 3U), 0);
data->grid_dim.resize(n_colors);
data->block_dim.resize(n_colors);
data->local_to_global.resize(n_colors);
data->constraint_mask.resize(n_colors);
data->row_start.resize(n_colors);
if (update_flags & update_quadrature_points)
data->q_points.resize(n_colors);
if (update_flags & update_JxW_values)
data->JxW.resize(n_colors);
if (update_flags & update_gradients)
data->inv_jacobian.resize(n_colors);
}
template <int dim, typename Number>
void
ReinitHelper<dim, Number>::setup_cell_arrays(const unsigned int color)
{
const unsigned int n_cells = data->n_cells[color];
const unsigned int cells_per_block = data->cells_per_block;
// Setup kernel parameters
const double apply_n_blocks = std::ceil(
static_cast<double>(n_cells) / static_cast<double>(cells_per_block));
const unsigned int apply_x_n_blocks =
std::round(std::sqrt(apply_n_blocks));
const unsigned int apply_y_n_blocks =
std::ceil(apply_n_blocks / static_cast<double>(apply_x_n_blocks));
data->grid_dim[color] = dim3(apply_x_n_blocks, apply_y_n_blocks);
// TODO this should be a templated parameter.
const unsigned int n_dofs_1d = fe_degree + 1;
if (data->parallelization_scheme ==
MatrixFree<dim, Number>::parallel_in_elem)
{
if (dim == 1)
data->block_dim[color] = dim3(n_dofs_1d * cells_per_block);
else if (dim == 2)
data->block_dim[color] =
dim3(n_dofs_1d * cells_per_block, n_dofs_1d);
else
data->block_dim[color] =
dim3(n_dofs_1d * cells_per_block, n_dofs_1d, n_dofs_1d);
}
else
data->block_dim[color] = dim3(cells_per_block);
local_to_global_host.resize(n_cells * padding_length);
if (update_flags & update_quadrature_points)
q_points_host.resize(n_cells * padding_length);
if (update_flags & update_JxW_values)
JxW_host.resize(n_cells * padding_length);
if (update_flags & update_gradients)
inv_jacobian_host.resize(n_cells * padding_length * dim * dim);
constraint_mask_host.resize(n_cells);
}
template <int dim, typename Number>
template <typename CellFilter>
void
ReinitHelper<dim, Number>::get_cell_data(
const CellFilter & cell,
const unsigned int cell_id,
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner)
{
cell->get_dof_indices(local_dof_indices);
// When using MPI, we need to transform the local_dof_indices, which
// contains global dof indices, to get local (to the current MPI process)
// dof indices.
if (partitioner)
for (auto &index : local_dof_indices)
index = partitioner->global_to_local(index);
for (unsigned int i = 0; i < dofs_per_cell; ++i)
lexicographic_dof_indices[i] = local_dof_indices[lexicographic_inv[i]];
const ArrayView<dealii::internal::MatrixFreeFunctions::ConstraintTypes>
cell_id_view(constraint_mask_host[cell_id]);
hanging_nodes.setup_constraints(cell,
partitioner,
lexicographic_inv,
lexicographic_dof_indices,
cell_id_view);
memcpy(&local_to_global_host[cell_id * padding_length],
lexicographic_dof_indices.data(),
dofs_per_cell * sizeof(types::global_dof_index));
fe_values.reinit(cell);
// Quadrature points
if (update_flags & update_quadrature_points)
{
const std::vector<Point<dim>> &q_points =
fe_values.get_quadrature_points();
std::copy(q_points.begin(),
q_points.end(),
&q_points_host[cell_id * padding_length]);
}
if (update_flags & update_JxW_values)
{
std::vector<double> JxW_values_double = fe_values.get_JxW_values();
const unsigned int offset = cell_id * padding_length;
for (unsigned int i = 0; i < q_points_per_cell; ++i)
JxW_host[i + offset] = static_cast<Number>(JxW_values_double[i]);
}
if (update_flags & update_gradients)
{
const std::vector<DerivativeForm<1, dim, dim>> &inv_jacobians =
fe_values.get_inverse_jacobians();
std::copy(&inv_jacobians[0][0][0],
&inv_jacobians[0][0][0] +
q_points_per_cell * sizeof(DerivativeForm<1, dim, dim>) /
sizeof(double),
&inv_jacobian_host[cell_id * padding_length * dim * dim]);
}
}
template <int dim, typename Number>
void
ReinitHelper<dim, Number>::alloc_and_copy_arrays(const unsigned int color)
{
const unsigned int n_cells = data->n_cells[color];
// Local-to-global mapping
if (data->parallelization_scheme ==
MatrixFree<dim, Number>::parallel_over_elem)
transpose_in_place(local_to_global_host, n_cells, padding_length);
alloc_and_copy(
&data->local_to_global[color],
ArrayView<const types::global_dof_index>(local_to_global_host.data(),
local_to_global_host.size()),
n_cells * padding_length);
// Quadrature points
if (update_flags & update_quadrature_points)
{
if (data->parallelization_scheme ==
MatrixFree<dim, Number>::parallel_over_elem)
transpose_in_place(q_points_host, n_cells, padding_length);
alloc_and_copy(&data->q_points[color],
ArrayView<const Point<dim, Number>>(
q_points_host.data(), q_points_host.size()),
n_cells * padding_length);
}
// Jacobian determinants/quadrature weights
if (update_flags & update_JxW_values)
{
if (data->parallelization_scheme ==
MatrixFree<dim, Number>::parallel_over_elem)
transpose_in_place(JxW_host, n_cells, padding_length);
alloc_and_copy(&data->JxW[color],
ArrayView<const Number>(JxW_host.data(),
JxW_host.size()),
n_cells * padding_length);
}
// Inverse jacobians
if (update_flags & update_gradients)
{
// Reorder so that all J_11 elements are together, all J_12 elements
// are together, etc., i.e., reorder indices from
// cell_id*q_points_per_cell*dim*dim + q*dim*dim +i to
// i*q_points_per_cell*n_cells + cell_id*q_points_per_cell+q
transpose_in_place(inv_jacobian_host,
padding_length * n_cells,
dim * dim);
// Transpose second time means we get the following index order:
// q*n_cells*dim*dim + i*n_cells + cell_id which is good for an
// element-level parallelization
if (data->parallelization_scheme ==
MatrixFree<dim, Number>::parallel_over_elem)
transpose_in_place(inv_jacobian_host,
n_cells * dim * dim,
padding_length);
alloc_and_copy(&data->inv_jacobian[color],
ArrayView<const Number>(inv_jacobian_host.data(),
inv_jacobian_host.size()),
n_cells * dim * dim * padding_length);
}
alloc_and_copy(
&data->constraint_mask[color],
ArrayView<const dealii::internal::MatrixFreeFunctions::ConstraintTypes>(
constraint_mask_host.data(), constraint_mask_host.size()),
n_cells);
}
template <int dim, typename number>
std::vector<types::global_dof_index>
get_conflict_indices(
const FilteredIterator<typename DoFHandler<dim>::active_cell_iterator>
& cell,
const AffineConstraints<number> &constraints)
{
std::vector<types::global_dof_index> local_dof_indices(
cell->get_fe().n_dofs_per_cell());
cell->get_dof_indices(local_dof_indices);
constraints.resolve_indices(local_dof_indices);
return local_dof_indices;
}
template <typename Number>
__global__ void
copy_constrained_dofs(
const dealii::types::global_dof_index *constrained_dofs,
const unsigned int n_constrained_dofs,
const unsigned int size,
const Number * src,
Number * dst)
{
const unsigned int dof =
threadIdx.x + blockDim.x * (blockIdx.x + gridDim.x * blockIdx.y);
// When working with distributed vectors, the constrained dofs are
// computed for ghosted vectors but we want to copy the values of the
// constrained dofs of non-ghosted vectors.
if ((dof < n_constrained_dofs) && (constrained_dofs[dof] < size))
dst[constrained_dofs[dof]] = src[constrained_dofs[dof]];
}
template <typename Number>
__global__ void
set_constrained_dofs(
const dealii::types::global_dof_index *constrained_dofs,
const unsigned int n_constrained_dofs,
const unsigned int size,
Number val,
Number * dst)
{
const unsigned int dof =
threadIdx.x + blockDim.x * (blockIdx.x + gridDim.x * blockIdx.y);
// When working with distributed vectors, the constrained dofs are
// computed for ghosted vectors but we want to set the values of the
// constrained dofs of non-ghosted vectors.
if ((dof < n_constrained_dofs) && (constrained_dofs[dof] < size))
dst[constrained_dofs[dof]] = val;
}
template <int dim, typename Number, typename Functor>
__global__ void
apply_kernel_shmem(Functor func,
const typename MatrixFree<dim, Number>::Data gpu_data,
const Number * src,
Number * dst)
{
constexpr unsigned int cells_per_block =
cells_per_block_shmem(dim, Functor::n_dofs_1d - 1);
constexpr unsigned int n_dofs_per_block =
cells_per_block * Functor::n_local_dofs;
constexpr unsigned int n_q_points_per_block =
cells_per_block * Functor::n_q_points;
// TODO make use of dynamically allocated shared memory
__shared__ Number values[n_dofs_per_block];
__shared__ Number gradients[dim][n_q_points_per_block];
const unsigned int local_cell = threadIdx.x / Functor::n_dofs_1d;
const unsigned int cell =
local_cell + cells_per_block * (blockIdx.x + gridDim.x * blockIdx.y);
Number *gq[dim];
for (int d = 0; d < dim; ++d)
gq[d] = &gradients[d][local_cell * Functor::n_q_points];
SharedData<dim, Number> shared_data(
&values[local_cell * Functor::n_local_dofs], gq);
if (cell < gpu_data.n_cells)
func(cell, &gpu_data, &shared_data, src, dst);
}
template <int dim, typename Number, typename Functor>
__global__ void
evaluate_coeff(Functor func,
const typename MatrixFree<dim, Number>::Data gpu_data)
{
constexpr unsigned int cells_per_block =
cells_per_block_shmem(dim, Functor::n_dofs_1d - 1);
const unsigned int local_cell = threadIdx.x / Functor::n_dofs_1d;
const unsigned int cell =
local_cell + cells_per_block * (blockIdx.x + gridDim.x * blockIdx.y);
if (cell < gpu_data.n_cells)
func(cell, &gpu_data);
}
} // namespace internal
template <int dim, typename Number>
MatrixFree<dim, Number>::MatrixFree()
: n_dofs(0)
, constrained_dofs(nullptr)
, padding_length(0)
, my_id(-1)
, dof_handler(nullptr)
{}
template <int dim, typename Number>
MatrixFree<dim, Number>::~MatrixFree()
{
free();
}
template <int dim, typename Number>
template <typename IteratorFiltersType>
void
MatrixFree<dim, Number>::reinit(const Mapping<dim> & mapping,
const DoFHandler<dim> & dof_handler,
const AffineConstraints<Number> &constraints,
const Quadrature<1> & quad,
const IteratorFiltersType &iterator_filter,
const AdditionalData & additional_data)
{
const auto &triangulation = dof_handler.get_triangulation();
if (const auto parallel_triangulation =
dynamic_cast<const parallel::TriangulationBase<dim> *>(
&triangulation))
internal_reinit(mapping,
dof_handler,
constraints,
quad,
iterator_filter,
std::make_shared<const MPI_Comm>(
parallel_triangulation->get_communicator()),
additional_data);
else
internal_reinit(mapping,
dof_handler,
constraints,
quad,
iterator_filter,
nullptr,
additional_data);
}
template <int dim, typename Number>
void
MatrixFree<dim, Number>::reinit(const Mapping<dim> & mapping,
const DoFHandler<dim> & dof_handler,
const AffineConstraints<Number> &constraints,
const Quadrature<1> & quad,
const AdditionalData &additional_data)
{
IteratorFilters::LocallyOwnedCell locally_owned_cell_filter;
reinit(mapping,
dof_handler,
constraints,
quad,
locally_owned_cell_filter,
additional_data);
}
template <int dim, typename Number>
void
MatrixFree<dim, Number>::reinit(const DoFHandler<dim> & dof_handler,
const AffineConstraints<Number> &constraints,
const Quadrature<1> & quad,
const AdditionalData &additional_data)
{
reinit(StaticMappingQ1<dim>::mapping,
dof_handler,
constraints,
quad,
additional_data);
}
template <int dim, typename Number>
MatrixFree<dim, Number>::Data
MatrixFree<dim, Number>::get_data(unsigned int color) const
{
Data data_copy;
data_copy.q_points = q_points[color];
data_copy.local_to_global = local_to_global[color];
data_copy.inv_jacobian = inv_jacobian[color];
data_copy.JxW = JxW[color];
data_copy.id = my_id;
data_copy.n_cells = n_cells[color];
data_copy.padding_length = padding_length;
data_copy.row_start = row_start[color];
data_copy.use_coloring = use_coloring;
data_copy.constraint_mask = constraint_mask[color];
return data_copy;
}
template <int dim, typename Number>
void
MatrixFree<dim, Number>::free()
{
for (auto &q_points_color_ptr : q_points)
Utilities::CUDA::free(q_points_color_ptr);
q_points.clear();
for (auto &local_to_global_color_ptr : local_to_global)
Utilities::CUDA::free(local_to_global_color_ptr);
local_to_global.clear();
for (auto &inv_jacobian_color_ptr : inv_jacobian)
Utilities::CUDA::free(inv_jacobian_color_ptr);
inv_jacobian.clear();
for (auto &JxW_color_ptr : JxW)
Utilities::CUDA::free(JxW_color_ptr);
JxW.clear();
for (auto &constraint_mask_color_ptr : constraint_mask)
Utilities::CUDA::free(constraint_mask_color_ptr);
constraint_mask.clear();
Utilities::CUDA::free(constrained_dofs);
internal::used_objects[my_id].store(false);
my_id = -1;
}
template <int dim, typename Number>
template <typename VectorType>
void
MatrixFree<dim, Number>::copy_constrained_values(const VectorType &src,
VectorType & dst) const
{
static_assert(
std::is_same<Number, typename VectorType::value_type>::value,
"VectorType::value_type and Number should be of the same type.");
if (partitioner)
distributed_copy_constrained_values(src, dst);
else
serial_copy_constrained_values(src, dst);
}
template <int dim, typename Number>
template <typename VectorType>
void
MatrixFree<dim, Number>::set_constrained_values(Number val,
VectorType &dst) const
{
static_assert(
std::is_same<Number, typename VectorType::value_type>::value,
"VectorType::value_type and Number should be of the same type.");
if (partitioner)
distributed_set_constrained_values(val, dst);
else
serial_set_constrained_values(val, dst);
}
template <int dim, typename Number>
void
MatrixFree<dim, Number>::initialize_dof_vector(
LinearAlgebra::CUDAWrappers::Vector<Number> &vec) const
{
vec.reinit(n_dofs);
}
template <int dim, typename Number>
void
MatrixFree<dim, Number>::initialize_dof_vector(
LinearAlgebra::distributed::Vector<Number, MemorySpace::CUDA> &vec) const
{
if (partitioner)
vec.reinit(partitioner);
else
vec.reinit(n_dofs);
}
template <int dim, typename Number>
unsigned int
MatrixFree<dim, Number>::get_padding_length() const
{
return padding_length;
}
template <int dim, typename Number>
template <typename Functor, typename VectorType>
void
MatrixFree<dim, Number>::cell_loop(const Functor & func,
const VectorType &src,
VectorType & dst) const
{
if (partitioner)
distributed_cell_loop(func, src, dst);
else
serial_cell_loop(func, src, dst);
}
template <int dim, typename Number>
template <typename Functor>
void
MatrixFree<dim, Number>::evaluate_coefficients(Functor func) const
{
for (unsigned int i = 0; i < n_colors; ++i)
if (n_cells[i] > 0)
{
internal::evaluate_coeff<dim, Number, Functor>
<<<grid_dim[i], block_dim[i]>>>(func, get_data(i));
AssertCudaKernel();
}
}
template <int dim, typename Number>
std::size_t
MatrixFree<dim, Number>::memory_consumption() const
{
// First compute the size of n_cells, row_starts, kernel launch parameters,
// and constrained_dofs
std::size_t bytes = n_cells.size() * sizeof(unsigned int) * 2 +
2 * n_colors * sizeof(dim3) +
n_constrained_dofs * sizeof(unsigned int);
// For each color, add local_to_global, inv_jacobian, JxW, and q_points.
for (unsigned int i = 0; i < n_colors; ++i)
{
bytes += n_cells[i] * padding_length * sizeof(unsigned int) +
n_cells[i] * padding_length * dim * dim * sizeof(Number) +
n_cells[i] * padding_length * sizeof(Number) +
n_cells[i] * padding_length * sizeof(point_type) +
n_cells[i] * sizeof(unsigned int);
}
return bytes;
}
template <int dim, typename Number>
template <typename IteratorFiltersType>
void
MatrixFree<dim, Number>::internal_reinit(
const Mapping<dim> & mapping,
const DoFHandler<dim> & dof_handler_,
const AffineConstraints<Number> &constraints,
const Quadrature<1> & quad,
const IteratorFiltersType & iterator_filter,
std::shared_ptr<const MPI_Comm> comm,
const AdditionalData additional_data)
{
dof_handler = &dof_handler_;
if (typeid(Number) == typeid(double))
cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeEightByte);
const UpdateFlags &update_flags = additional_data.mapping_update_flags;
if (additional_data.parallelization_scheme != parallel_over_elem &&
additional_data.parallelization_scheme != parallel_in_elem)
AssertThrow(false, ExcMessage("Invalid parallelization scheme."));
this->parallelization_scheme = additional_data.parallelization_scheme;
this->use_coloring = additional_data.use_coloring;
this->overlap_communication_computation =
additional_data.overlap_communication_computation;
// TODO: only free if we actually need arrays of different length
free();
n_dofs = dof_handler->n_dofs();
const FiniteElement<dim> &fe = dof_handler->get_fe();
fe_degree = fe.degree;
// TODO this should be a templated parameter
const unsigned int n_dofs_1d = fe_degree + 1;
const unsigned int n_q_points_1d = quad.size();
Assert(n_dofs_1d == n_q_points_1d,
ExcMessage("n_q_points_1d must be equal to fe_degree+1."));
// Set padding length to the closest power of two larger than or equal to
// the number of threads.
padding_length = 1 << static_cast<unsigned int>(
std::ceil(dim * std::log2(fe_degree + 1.)));
dofs_per_cell = fe.n_dofs_per_cell();
q_points_per_cell = std::pow(n_q_points_1d, dim);
const ::dealii::internal::MatrixFreeFunctions::ShapeInfo<Number> shape_info(
quad, fe);
unsigned int size_shape_values = n_dofs_1d * n_q_points_1d * sizeof(Number);
FE_DGQArbitraryNodes<1> fe_quad_co(quad);
const ::dealii::internal::MatrixFreeFunctions::ShapeInfo<Number>
shape_info_co(quad, fe_quad_co);
unsigned int size_co_shape_values =
n_q_points_1d * n_q_points_1d * sizeof(Number);
// Check if we already a part of the constant memory allocated to us. If
// not, we try to get a block of memory.
bool found_id = false;
while (!found_id)
{
++my_id;
Assert(
my_id < static_cast<int>(mf_n_concurrent_objects),
ExcMessage(
"Maximum number of concurrent MatrixFree objects reached. Increase mf_n_concurrent_objects"));
bool f = false;
found_id =
internal::used_objects[my_id].compare_exchange_strong(f, true);
}
cudaError_t cuda_error =
cudaMemcpyToSymbol(internal::get_global_shape_values<Number>(0),
shape_info.data.front().shape_values.data(),
size_shape_values,
my_id * internal::data_array_size * sizeof(Number),
cudaMemcpyHostToDevice);
AssertCuda(cuda_error);
if (update_flags & update_gradients)
{
cuda_error =
cudaMemcpyToSymbol(internal::get_global_shape_gradients<Number>(0),
shape_info.data.front().shape_gradients.data(),
size_shape_values,
my_id * internal::data_array_size * sizeof(Number),
cudaMemcpyHostToDevice);
AssertCuda(cuda_error);
cuda_error =
cudaMemcpyToSymbol(internal::get_global_co_shape_gradients<Number>(0),
shape_info_co.data.front().shape_gradients.data(),
size_co_shape_values,
my_id * internal::data_array_size * sizeof(Number),
cudaMemcpyHostToDevice);
AssertCuda(cuda_error);
}
// Setup the number of cells per CUDA thread block
cells_per_block = cells_per_block_shmem(dim, fe_degree);
internal::ReinitHelper<dim, Number> helper(
this, mapping, fe, quad, shape_info, *dof_handler, update_flags);
// Create a graph coloring
CellFilter begin(iterator_filter, dof_handler->begin_active());
CellFilter end(iterator_filter, dof_handler->end());
if (begin != end)
{
if (additional_data.use_coloring)
{
const auto fun = [&](const CellFilter &filter) {
return internal::get_conflict_indices<dim, Number>(filter,