Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MeshMaterialVariableScalar.inst.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MeshMaterialVariableScalar.cc (C) 2000-2024 */
9/* */
10/* Scalar variable on a mesh material. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/materials/MeshMaterialVariable.h"
15
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/ScopedPtr.h"
18#include "arcane/utils/ITraceMng.h"
19#include "arcane/utils/NumericTypes.h"
20#include "arcane/utils/CheckedConvert.h"
21
22#include "arcane/materials/MaterialVariableBuildInfo.h"
23#include "arcane/materials/IMeshMaterial.h"
24#include "arcane/materials/MatItemEnumerator.h"
25#include "arcane/materials/MeshMaterialVariableSynchronizerList.h"
26#include "arcane/materials/ItemMaterialVariableBaseT.H"
27#include "arcane/materials/IMeshMaterialVariableSynchronizer.h"
28#include "arcane/materials/internal/MeshMaterialVariablePrivate.h"
29
30#include "arcane/core/IItemFamily.h"
31#include "arcane/core/IMesh.h"
32#include "arcane/core/IParallelMng.h"
33#include "arcane/core/ISubDomain.h"
34#include "arcane/core/IApplication.h"
35#include "arcane/core/IDataFactoryMng.h"
36#include "arcane/core/IParallelExchanger.h"
37#include "arcane/core/ISerializer.h"
38#include "arcane/core/ISerializeMessage.h"
39#include "arcane/core/internal/IVariableInternal.h"
40#include "arcane/core/materials/internal/IMeshComponentInternal.h"
41#include "arcane/core/VariableInfo.h"
42#include "arcane/core/VariableRefArray.h"
43#include "arcane/core/MeshVariable.h"
44#include "arcane/core/VariableArray.h"
45#include "arcane/core/ItemPrinter.h"
46#include "arcane/core/IVariableSynchronizer.h"
47#include "arcane/core/internal/IDataInternal.h"
48#include "arcane/core/Timer.h"
49#include "arcane/core/parallel/IStat.h"
50#include "arcane/core/datatype/DataTypeTraits.h"
51#include "arcane/core/datatype/DataStorageBuildInfo.h"
52
53#include <vector>
54
55/*---------------------------------------------------------------------------*/
56/*---------------------------------------------------------------------------*/
57
58namespace Arcane::Materials
59{
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
64template <typename DataType> void
66saveData(IMeshComponent* component, IData* data,
67 Array<ContainerViewType>& cviews)
68{
69 ConstArrayView<ArrayView<DataType>> views = cviews;
70 auto* true_data = dynamic_cast<ValueDataType*>(data);
71 ARCANE_CHECK_POINTER(true_data);
72 ContainerType& values = true_data->_internal()->_internalDeprecatedValue();
73 ENUMERATE_COMPONENTCELL (icell, component) {
74 MatVarIndex mvi = icell._varIndex();
75 values.add(views[mvi.arrayIndex()][mvi.valueIndex()]);
76 }
77}
78
79/*---------------------------------------------------------------------------*/
80/*---------------------------------------------------------------------------*/
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85template <typename DataType> void
87copyTo(SmallSpan<const DataType> input, SmallSpan<const Int32> input_indexes,
88 SmallSpan<DataType> output, SmallSpan<const Int32> output_indexes,
89 const RunQueue& queue)
90{
91 // TODO: check if index sizes are identical
92 Integer nb_value = input_indexes.size();
93 auto command = makeCommand(queue);
94 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output.data());
95 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input.data());
96 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, input_indexes.data());
97 ARCANE_CHECK_ACCESSIBLE_POINTER(queue, output_indexes.data());
98 command << RUNCOMMAND_LOOP1(iter, nb_value)
99 {
100 auto [i] = iter();
101 output[output_indexes[i]] = input[input_indexes[i]];
102 };
103}
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108template <typename DataType> void
110resizeAndFillWithDefault(ValueDataType* data, ContainerType& container, Integer dim1_size)
111{
112 ARCANE_UNUSED(data);
113 //TODO: create an Array2 version that specifies a value to give
114 // for initialization during a resize() (like Array::resize()).
115 container.resize(dim1_size, DataType());
116}
117
118/*---------------------------------------------------------------------------*/
119/*---------------------------------------------------------------------------*/
120
121template <typename DataType> void
123resizeWithReserve(PrivatePartType* var, Integer dim1_size, Real reserve_ratio)
124{
125 // To avoid reallocating every time there is an increase in
126 // the number of material cells, allocate a little more than necessary.
127 // By default, we allocate 5% more.
128 Int32 nb_add = static_cast<Int32>(dim1_size * reserve_ratio);
129 var->_internalApi()->resize(VariableResizeArgs(dim1_size, nb_add, true));
130}
131
132/*---------------------------------------------------------------------------*/
133/*---------------------------------------------------------------------------*/
134
135template <typename DataType> ItemMaterialVariableScalar<DataType>::
137 PrivatePartType* global_var,
138 VariableRef* global_var_ref, MatVarSpace mvs)
139: BaseClass(v, global_var, global_var_ref, mvs)
140{
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
148
161template <typename DataType> void
162ItemMaterialVariableScalar<DataType>::
163fillFromArray(IMeshMaterial* mat, ConstArrayView<DataType> values)
164{
165 // TODO: make a version with IMeshComponent
166 Integer index = 0;
167 ENUMERATE_COMPONENTITEM (MatCell, imatcell, mat) {
168 MatCell mc = *imatcell;
169 MatVarIndex mvi = mc._varIndex();
170 setValue(mvi, values[index]);
171 ++index;
172 }
173}
174
175/*---------------------------------------------------------------------------*/
176/*---------------------------------------------------------------------------*/
177
190template <typename DataType> void
191ItemMaterialVariableScalar<DataType>::
192fillFromArray(IMeshMaterial* mat, ConstArrayView<DataType> values,
193 Int32ConstArrayView indexes)
194{
195 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
196 Integer nb_index = indexes.size();
197 for (Integer i = 0; i < nb_index; ++i) {
198 MatVarIndex mvi = mat_indexes[indexes[i]];
199 setValue(mvi, values[i]);
200 }
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
218template <typename DataType> void
219ItemMaterialVariableScalar<DataType>::
220fillToArray(IMeshMaterial* mat, ArrayView<DataType> values)
221{
222 Integer index = 0;
223 ENUMERATE_COMPONENTITEM (MatCell, imatcell, mat) {
224 MatCell mc = *imatcell;
225 MatVarIndex mvi = mc._varIndex();
226 values[index] = this->operator[](mvi);
227 ++index;
228 }
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
246template <typename DataType> void
247ItemMaterialVariableScalar<DataType>::
248fillToArray(IMeshMaterial* mat, ArrayView<DataType> values, Int32ConstArrayView indexes)
249{
250 ConstArrayView<MatVarIndex> mat_indexes = mat->_internalApi()->variableIndexer()->matvarIndexes();
251 Integer nb_index = indexes.size();
252 for (Integer i = 0; i < nb_index; ++i) {
253 MatVarIndex mvi = mat_indexes[indexes[i]];
254 values[i] = this->operator[](mvi);
255 }
256}
257
258/*---------------------------------------------------------------------------*/
259/*---------------------------------------------------------------------------*/
260
264template <typename DataType> void
265ItemMaterialVariableScalar<DataType>::
266fillPartialValues(const DataType& value)
267{
268 // The index 0 variable corresponds to the global variable.
269 // It should therefore not be taken into account.
270 Integer nb_var = m_vars.size();
271 for (Integer i = 1; i < nb_var; ++i) {
272 if (m_vars[i])
273 m_vars[i]->fill(value);
274 }
275}
276
277/*---------------------------------------------------------------------------*/
278/*---------------------------------------------------------------------------*/
279
280template <typename DataType> Int32
281ItemMaterialVariableScalar<DataType>::
282dataTypeSize() const
283{
284 return (Int32)sizeof(DataType);
285}
286
287/*---------------------------------------------------------------------------*/
288/*---------------------------------------------------------------------------*/
289
290template <typename DataType> void
291ItemMaterialVariableScalar<DataType>::
292_copyToBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<std::byte> bytes) const
293{
294 // TODO: Check that the size is a multiple of sizeof(DataType) and that
295 // the alignment is correct.
296 const Integer value_size = arcaneCheckArraySize(bytes.size() / sizeof(DataType));
297 ArrayView<DataType> values(value_size, reinterpret_cast<DataType*>(bytes.data()));
298 for (Integer z = 0; z < value_size; ++z) {
299 values[z] = this->operator[](matvar_indexes[z]);
300 }
301}
302
303/*---------------------------------------------------------------------------*/
304/*---------------------------------------------------------------------------*/
305
306template <typename DataType> void
307ItemMaterialVariableScalar<DataType>::
308copyToBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteArrayView bytes) const
309{
310 auto* ptr = reinterpret_cast<std::byte*>(bytes.data());
311 return _copyToBufferLegacy(matvar_indexes, { ptr, bytes.size() });
312}
313
314/*---------------------------------------------------------------------------*/
315/*---------------------------------------------------------------------------*/
316
317template <typename DataType> void
318ItemMaterialVariableScalar<DataType>::
319_copyFromBufferLegacy(SmallSpan<const MatVarIndex> matvar_indexes, Span<const std::byte> bytes)
320{
321 // TODO: Check that the size is a multiple of sizeof(DataType) and that
322 // the alignment is correct.
323 const Int32 value_size = CheckedConvert::toInt32(bytes.size() / sizeof(DataType));
324 ConstArrayView<DataType> values(value_size, reinterpret_cast<const DataType*>(bytes.data()));
325 for (Integer z = 0; z < value_size; ++z) {
326 setValue(matvar_indexes[z], values[z]);
327 }
328}
329
330/*---------------------------------------------------------------------------*/
331/*---------------------------------------------------------------------------*/
332
333template <typename DataType> void
334ItemMaterialVariableScalar<DataType>::
335copyFromBuffer(ConstArrayView<MatVarIndex> matvar_indexes, ByteConstArrayView bytes)
336{
337 auto* ptr = reinterpret_cast<const std::byte*>(bytes.data());
338 return _copyFromBufferLegacy(matvar_indexes, { ptr, bytes.size() });
339}
340
341/*---------------------------------------------------------------------------*/
342/*---------------------------------------------------------------------------*/
343
344template <typename DataType> void
345ItemMaterialVariableScalar<DataType>::
346synchronize()
347{
348 IParallelMng* pm = m_p->materialMng()->mesh()->parallelMng();
349 Timer timer(pm->timerMng(), "MatTimer", Timer::TimerReal);
350 Int64 message_size = 0;
351 {
352 Timer::Sentry ts(&timer);
353 message_size = _synchronize2();
354 }
355 pm->stat()->add("MaterialSync", timer.lastActivationTime(), message_size);
356}
357
358/*---------------------------------------------------------------------------*/
359/*---------------------------------------------------------------------------*/
360
361template <typename DataType> Int64
362ItemMaterialVariableScalar<DataType>::
363_synchronize2()
364{
365 Int64 message_size = 0;
366 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
367 // Only versions 6 and later are available for medium variables.
368 if (m_p->space() == MatVarSpace::Environment) {
369 if (sync_version < 6)
370 sync_version = 6;
371 }
372 if (sync_version >= 6) {
373 MeshMaterialVariableSynchronizerList mmvsl(m_p->materialMng());
374 mmvsl.add(this);
375 mmvsl.apply();
376 message_size = mmvsl.totalMessageSize();
377 }
378 else if (sync_version == 5 || sync_version == 4 || sync_version == 3) {
379 _synchronizeV5();
380 }
381 else if (sync_version == 2) {
382 _synchronizeV2();
383 }
384 else
385 _synchronizeV1();
386 return message_size;
387}
388
389/*---------------------------------------------------------------------------*/
390/*---------------------------------------------------------------------------*/
391
392template <typename DataType> void
393ItemMaterialVariableScalar<DataType>::
394synchronize(MeshMaterialVariableSynchronizerList& sync_list)
395{
396 Integer sync_version = m_p->materialMng()->synchronizeVariableVersion();
397 if (sync_version >= 6) {
398 sync_list.add(this);
399 }
400 else
401 this->synchronize();
402}
403
404/*---------------------------------------------------------------------------*/
405/*---------------------------------------------------------------------------*/
406
407template <typename DataType> void
408ItemMaterialVariableScalar<DataType>::
409_synchronizeV1()
410{
411 // Synchronization:
412 // For now, an unoptimized algorithm that performs multiple
413 // syncs. We use the global variable for this and must
414 // therefore save it at the beginning of the function and restore it at the end.
415 // The principle is simple: for each material and environment, we
416 // copy the corresponding partial value into the global variable
417 // and synchronize the global variable. Then we must
418 // copy from the global value into the partial value.
419 // This means we have as many synchronizations as there are materials and environments.
420
421 // TODO: check that the list of materials is consistent between
422 // subdomains. This is not necessary with this simplified algorithm
423 // but it will be with the final algorithm. For this test, it is enough to put
424 // a special value in the global variable for each synchronization
425 // (for example MIN_FLOAT) and after synchronization, check for each
426 // cell whose value is different from MIN_FLOAT
427 // if it is indeed in our material
428 IMeshMaterialMng* material_mng = m_p->materialMng();
429 IMesh* mesh = material_mng->mesh();
430 //TODO: Use a type other than cellFamily() to allow for other types
431 // of elements.
432 IItemFamily* family = mesh->cellFamily();
433 IParallelMng* pm = mesh->parallelMng();
434 if (!pm->isParallel())
435 return;
436 ItemGroup all_items = family->allItems();
437
438 UniqueArray<DataType> saved_values;
439 // Save the global variable values because we are going to change them
440 {
441 ConstArrayView<DataType> var_values = m_global_variable->valueView();
442 saved_values.resize(var_values.size());
443 saved_values.copy(var_values);
444 }
445
446 ENUMERATE_ENV (ienv, material_mng) {
447 IMeshEnvironment* env = *ienv;
448 ENUMERATE_MAT (imat, env) {
449 IMeshMaterial* mat = *imat;
450
451 {
452 ArrayView<DataType> var_values = m_global_variable->valueView();
453 // Copy material values into the global variable then synchronize
454 ENUMERATE_MATCELL (imatcell, mat) {
455 MatCell mc = *imatcell;
456 Cell c = mc.globalCell();
457 MatVarIndex mvi = mc._varIndex();
458 var_values[c.localId()] = this->operator[](mvi);
459 }
460 }
461 m_global_variable->synchronize();
462 {
463 ConstArrayView<DataType> var_values = m_global_variable->valueView();
464 // Copy values from the global variable into the material part
465 ENUMERATE_MATCELL (imatcell, mat) {
466 MatCell mc = *imatcell;
467 Cell c = mc.globalCell();
468 MatVarIndex mvi = mc._varIndex();
469 setValue(mvi, var_values[c.localId()]);
470 }
471 }
472 }
473
474 // Do the same for the environment
475 {
476 ArrayView<DataType> var_values = m_global_variable->valueView();
477 // Copy environment values into the global variable then synchronize
478 ENUMERATE_ENVCELL (ienvcell, env) {
479 EnvCell ec = *ienvcell;
480 Cell c = ec.globalCell();
481 MatVarIndex mvi = ec._varIndex();
482 var_values[c.localId()] = this->operator[](mvi);
483 }
484 }
485 m_global_variable->synchronize();
486 {
487 ConstArrayView<DataType> var_values = m_global_variable->valueView();
488 // Copy values from the global variable into the environment part
489 ENUMERATE_ENVCELL (ienvcell, env) {
490 EnvCell ec = *ienvcell;
491 Cell c = ec.globalCell();
492 MatVarIndex mvi = ec._varIndex();
493 setValue(mvi, var_values[c.localId()]);
494 }
495 }
496 }
497
498 // Restore the global variable values.
499 {
500 ArrayView<DataType> var_values = m_global_variable->valueView();
501 var_values.copy(saved_values);
502 }
503 m_global_variable->synchronize();
504}
505
506/*---------------------------------------------------------------------------*/
507/*---------------------------------------------------------------------------*/
508
509template <typename DataType> void
510ItemMaterialVariableScalar<DataType>::
511_synchronizeV2()
512{
513 // Synchronization:
514 // For now, non-optimized algorithm that performs several
515 // synchronizations. We use the global variable for this and must
516 // therefore save it at the beginning of the function and restore it at the end.
517 // The principle is simple: for each material and environment, we
518 // copy the corresponding partial value into the global variable
519 // and synchronize the global variable. Then we must
520 // copy from the global value into the partial value.
521 // This means we have as many synchronizations as there are materials and environments.
522
523 // TODO: check that the list of materials is consistent between
524 // subdomains. This is not necessary with this simplified algorithm
525 // but it will be with the final algorithm. For this test, it is enough to put
526 // a special value in the global variable for each synchronization
527 // (for example MIN_FLOAT) and after synchronization, check for each
528 // cell whose value is different from MIN_FLOAT
529 // if it is indeed in our material
530 IMeshMaterialMng* material_mng = m_p->materialMng();
531 IMesh* mesh = material_mng->mesh();
532 //TODO: Use a type other than cellFamily() to allow for other types
533 // of elements.
534 IItemFamily* family = mesh->cellFamily();
535 IParallelMng* pm = mesh->parallelMng();
536 if (!pm->isParallel())
537 return;
538 ItemGroup all_items = family->allItems();
539 ITraceMng* tm = pm->traceMng();
540 tm->info(4) << "MAT_SYNCHRONIZE_V2 name=" << this->name();
541 IDataFactoryMng* df = m_global_variable->dataFactoryMng();
542
543 DataStorageTypeInfo storage_type_info(VariableInfo::_internalGetStorageTypeInfo(m_global_variable->dataType(), 2, 0));
544 DataStorageBuildInfo storage_build_info(tm);
545 String storage_full_type = storage_type_info.fullName();
546
547 Ref<IData> xdata(df->createSimpleDataRef(storage_full_type, storage_build_info));
548 auto* data = dynamic_cast<IArray2DataT<DataType>*>(xdata.get());
549 if (!data)
550 ARCANE_FATAL("Bad type");
551
552 ConstArrayView<MeshMaterialVariableIndexer*> indexers = material_mng->_internalApi()->variablesIndexer();
553 Integer nb_indexer = indexers.size();
554 data->_internal()->_internalDeprecatedValue().resize(family->maxLocalId(), nb_indexer + 1);
555 Array2View<DataType> values(data->view());
556
557 // Copy partial values into the array.
558 for (MeshMaterialVariableIndexer* indexer : indexers) {
559 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
560 ConstArrayView<Int32> local_ids = indexer->localIds();
561 for (Integer j = 0, n = matvar_indexes.size(); j < n; ++j) {
562 MatVarIndex mvi = matvar_indexes[j];
563 values[local_ids[j]][mvi.arrayIndex()] = this->operator[](mvi);
564 }
565 }
566 family->allItemsSynchronizer()->synchronizeData(data);
567
568 // Copy the synchronized array into the partial values.
569 for (MeshMaterialVariableIndexer* indexer : indexers) {
570 ConstArrayView<MatVarIndex> matvar_indexes = indexer->matvarIndexes();
571 ConstArrayView<Int32> local_ids = indexer->localIds();
572 for (Integer j = 0, n = matvar_indexes.size(); j < n; ++j) {
573 MatVarIndex mvi = matvar_indexes[j];
574 setValue(mvi, values[local_ids[j]][mvi.arrayIndex()]);
575 }
576 }
577
578 m_global_variable->synchronize();
579}
580
581/*---------------------------------------------------------------------------*/
582/*---------------------------------------------------------------------------*/
583
584template <typename DataType> void
585ItemMaterialVariableScalar<DataType>::
586_synchronizeV5()
587{
588 // Synchronization version that sends only
589 // the values of materials and environments for shared cells.
590 // NOTE: This version requires that the materials are correctly
591 // synchronized between subdomains.
592
593 // This version is similar to V4 in principle but
594 // performs send/receive directly without using serialization.
595 IMeshMaterialMng* material_mng = m_p->materialMng();
596
597 IMeshMaterialVariableSynchronizer* mmvs = material_mng->_internalApi()->allCellsMatEnvSynchronizer();
598 IVariableSynchronizer* var_syncer = mmvs->variableSynchronizer();
599 IParallelMng* pm = var_syncer->parallelMng();
600
601 if (!pm->isParallel())
602 return;
603
604 mmvs->checkRecompute();
605
606 //ItemGroup all_items = family->allItems();
607 ITraceMng* tm = pm->traceMng();
608 tm->info(4) << "MAT_SYNCHRONIZE_V5 name=" << this->name();
609 //tm->info() << "STACK="<< platform::getStackTrace();
610 const Integer data_type_size = (Integer)sizeof(DataType);
611 {
612 Int32ConstArrayView ranks = var_syncer->communicatingRanks();
613 Integer nb_rank = ranks.size();
614 std::vector<UniqueArray<DataType>> shared_values(nb_rank);
615 std::vector<UniqueArray<DataType>> ghost_values(nb_rank);
616
617 UniqueArray<Parallel::Request> requests;
618
619 // Post the receives.
620 Int32UniqueArray recv_ranks(nb_rank);
621 for (Integer i = 0; i < nb_rank; ++i) {
622 Int32 rank = ranks[i];
623 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
624 Integer total = ghost_matcells.size();
625 ghost_values[i].resize(total);
626 Integer total_byte = CheckedConvert::multiply(total, data_type_size);
627 ByteArrayView bytes(total_byte, (Byte*)(ghost_values[i].unguardedBasePointer()));
628 requests.add(pm->recv(bytes, rank, false));
629 }
630
631 // Post the sends
632 for (Integer i = 0; i < nb_rank; ++i) {
633 Int32 rank = ranks[i];
634 ConstArrayView<MatVarIndex> shared_matcells(mmvs->sharedItems(i));
635 Integer total = shared_matcells.size();
636 shared_values[i].resize(total);
637 ArrayView<DataType> values(shared_values[i]);
638 for (Integer z = 0; z < total; ++z) {
639 values[z] = this->operator[](shared_matcells[z]);
640 }
641 Integer total_byte = CheckedConvert::multiply(total, data_type_size);
642 ByteArrayView bytes(total_byte, (Byte*)(values.unguardedBasePointer()));
643 requests.add(pm->send(bytes, rank, false));
644 }
645
646 pm->waitAllRequests(requests);
647
648 // Copy the received data into the ghost cells.
649 for (Integer i = 0; i < nb_rank; ++i) {
650 ConstArrayView<MatVarIndex> ghost_matcells(mmvs->ghostItems(i));
651 Integer total = ghost_matcells.size();
652 ConstArrayView<DataType> values(ghost_values[i].constView());
653 for (Integer z = 0; z < total; ++z) {
654 setValue(ghost_matcells[z], values[z]);
655 }
656 }
657 }
658}
659
660/*---------------------------------------------------------------------------*/
661/*---------------------------------------------------------------------------*/
662
663template <typename DataType> void
664ItemMaterialVariableScalar<DataType>::
665dumpValues(std::ostream& ostr, AllEnvCellVectorView view)
666{
667 ostr << "Dumping values for material variable name=" << this->name() << '\n';
668 ENUMERATE_ALLENVCELL (iallenvcell, view) {
669 AllEnvCell all_env_cell = *iallenvcell;
670 ostr << "Cell uid=" << ItemPrinter(all_env_cell.globalCell()) << " v=" << value(all_env_cell._varIndex()) << '\n';
671 for (CellComponentCellEnumerator ienvcell(all_env_cell); ienvcell.hasNext(); ++ienvcell) {
672 MatVarIndex evi = ienvcell._varIndex();
673 ostr << "env_value=" << value(evi) << ", mvi=" << evi << '\n';
674 for (CellComponentCellEnumerator imatcell(*ienvcell); imatcell.hasNext(); ++imatcell) {
675 MatVarIndex mvi = imatcell._varIndex();
676 ostr << "mat_value=" << value(mvi) << ", mvi=" << mvi << '\n';
677 }
678 }
679 }
680}
681
682/*---------------------------------------------------------------------------*/
683/*---------------------------------------------------------------------------*/
684
685template <typename DataType> void
686ItemMaterialVariableScalar<DataType>::
687dumpValues(std::ostream& ostr)
688{
689 ostr << "Dumping values for material variable name=" << this->name() << '\n';
690 IItemFamily* family = m_global_variable->itemFamily();
691 if (!family)
692 return;
693 IMeshMaterialMng* material_mng = m_p->materialMng();
694 dumpValues(ostr, material_mng->view(family->allItems()));
695}
696
697/*---------------------------------------------------------------------------*/
698/*---------------------------------------------------------------------------*/
699
700template <typename DataType> void
701ItemMaterialVariableScalar<DataType>::
702serialize(ISerializer* sbuf, Int32ConstArrayView ids)
703{
704 IItemFamily* family = m_global_variable->itemFamily();
705 if (!family)
706 return;
707 ITraceMng* tm = family->traceMng();
708 IMeshMaterialMng* mat_mng = m_p->materialMng();
709 const Integer nb_count = DataTypeTraitsT<DataType>::nbBasicType();
710 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
711 const eBasicDataType data_type = DataTypeTraitsT<BasicType>::basicDataType();
712 ItemVectorView ids_view(family->view(ids));
713 bool has_mat = this->space() != MatVarSpace::Environment;
714 switch (sbuf->mode()) {
715 case ISerializer::ModeReserve: {
716 Integer nb_val = 0;
717 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
718 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
719 EnvCell envcell = *ienvcell;
720 ++nb_val; // 1 value for the environment
721 if (has_mat)
722 nb_val += envcell.nbMaterial(); // 1 value per material in the environment.
723 }
724 }
725 tm->info() << "RESERVE: nb_value=" << 1 << " size=" << (nb_val * nb_count);
726 sbuf->reserveInt64(1); // For the number of values.
727 sbuf->reserveSpan(data_type, nb_val * nb_count); // For the number of values.
728 } break;
729 case ISerializer::ModePut: {
730 UniqueArray<DataType> values;
731 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
732 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
733 values.add(value(ienvcell._varIndex()));
734 if (has_mat) {
735 ENUMERATE_CELL_MATCELL (imatcell, (*ienvcell)) {
736 values.add(value(imatcell._varIndex()));
737 }
738 }
739 }
740 }
741 Integer nb_value = values.size();
742 ConstArrayView<BasicType> basic_values(nb_value * nb_count, reinterpret_cast<BasicType*>(values.data()));
743 tm->info() << "PUT: nb_value=" << nb_value << " size=" << basic_values.size();
744 sbuf->putInt64(nb_value);
745 sbuf->putSpan(basic_values);
746 } break;
747 case ISerializer::ModeGet: {
748 UniqueArray<BasicType> basic_values;
749 Int64 nb_value = sbuf->getInt64();
750 basic_values.resize(nb_value * nb_count);
751 sbuf->getSpan(basic_values);
752 Span<const DataType> data_values(reinterpret_cast<DataType*>(basic_values.data()), nb_value);
753 Integer index = 0;
754 ENUMERATE_ALLENVCELL (iallenvcell, mat_mng, ids_view) {
755 ENUMERATE_CELL_ENVCELL (ienvcell, (*iallenvcell)) {
756 EnvCell envcell = *ienvcell;
757 setValue(ienvcell._varIndex(), data_values[index]);
758 ++index;
759 if (has_mat) {
760 ENUMERATE_CELL_MATCELL (imatcell, envcell) {
761 setValue(imatcell._varIndex(), data_values[index]);
762 ++index;
763 }
764 }
765 }
766 }
767 } break;
768 default:
769 throw NotSupportedException(A_FUNCINFO, "Invalid serialize");
770 }
771}
772
773/*---------------------------------------------------------------------------*/
774/*---------------------------------------------------------------------------*/
775
776/*---------------------------------------------------------------------------*/
777/*---------------------------------------------------------------------------*/
778
779template <typename ItemType, typename DataType>
780MeshMaterialVariableScalar<ItemType, DataType>::
781MeshMaterialVariableScalar(const MaterialVariableBuildInfo& v, PrivatePartType* global_var,
782 VariableRefType* global_var_ref, MatVarSpace mvs)
783: ItemMaterialVariableScalar<DataType>(v, global_var, global_var_ref, mvs)
784, m_true_global_variable_ref(global_var_ref) // Will be destroyed by the base class
785{
786}
787
788/*---------------------------------------------------------------------------*/
789/*---------------------------------------------------------------------------*/
790
791template <typename ItemType, typename DataType>
792MeshMaterialVariableScalar<ItemType, DataType>::
793~MeshMaterialVariableScalar()
794{
795}
796
797/*---------------------------------------------------------------------------*/
798/*---------------------------------------------------------------------------*/
799
800/*---------------------------------------------------------------------------*/
801/*---------------------------------------------------------------------------*/
802
803#define ARCANE_INSTANTIATE_MAT(type) \
804 template class ItemMaterialVariableBase<MaterialVariableScalarTraits<type>>; \
805 template class ItemMaterialVariableScalar<type>; \
806 template class MeshMaterialVariableScalar<Cell, type>; \
807 template class MeshMaterialVariableCommonStaticImpl<MeshMaterialVariableScalar<Cell, type>>
808
809/*---------------------------------------------------------------------------*/
810/*---------------------------------------------------------------------------*/
811
812} // namespace Arcane::Materials
813
814/*---------------------------------------------------------------------------*/
815/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
1D loop on accelerator with additional arguments.
Interface of a component (material or environment) of a mesh.
Represents an index on material and environment variables.
Characteristics for a scalar material variable.
#define ENUMERATE_ENV(ienv, container)
Macro to iterate over a list of environments.
#define ENUMERATE_ENVCELL(iname, env)
Macro to iterate over all EnvCell cells of an environment.
#define ENUMERATE_CELL_MATCELL(iname, env_cell)
Macro to iterate over all MatCell cells of a cell.
#define ENUMERATE_CELL_ENVCELL(iname, all_env_cell)
Macro to iterate over all EnvCell cells of a cell.
#define ENUMERATE_COMPONENTITEM(ClassName_, iname, container,...)
Generic macro to iterate over entities of a material or an environment.
#define ENUMERATE_MATCELL(iname, mat)
Macro to iterate over all MatCell cells of a material.
#define ENUMERATE_ALLENVCELL(iname,...)
Macro to iterate over all AllEnvCell cells of a group.
#define ENUMERATE_COMPONENTCELL(iname, component)
Macro to iterate over all ComponentCell cells of a component.
#define ENUMERATE_MAT(imat, container)
Macro to iterate over a list of materials.
RunCommand makeCommand(const RunQueue &run_queue)
Creates a command associated with the queue run_queue.
Always enables tracing in Arcane parts concerning materials.
MatVarSpace
Definition space for a material variable.
Integer arcaneCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as the size of an array....
ArrayView< Byte > ByteArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:447
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
eBasicDataType
Type of a basic data item.
UniqueArray< Int32 > Int32UniqueArray
Dynamic 1D array of 32-bit integers.
Definition UtilsTypes.h:341
double Real
Type representing a real number.
@ Cell
The mesh is AMR by cell.
Definition MeshKind.h:53
std::int32_t Int32
Signed integer type of 32 bits.