Skip to content

Commit bab6903

Browse files
Merge pull request #1142 from arcaneframework/dev/gg-use-refcounter-for-basic-reader-and-writer
Use reference counter for fields of 'BasicReader' and 'BasicWriter'
2 parents c5644af + f6601ee commit bab6903

File tree

3 files changed

+41
-79
lines changed

3 files changed

+41
-79
lines changed

Diff for: arcane/src/arcane/std/BasicReader.cc

+12-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* BasicReader.cc (C) 2000-2023 */
8+
/* BasicReader.cc (C) 2000-2024 */
99
/* */
1010
/* Lecture simple pour les protections/reprises. */
1111
/*---------------------------------------------------------------------------*/
@@ -19,6 +19,7 @@
1919
#include "arcane/utils/JSONReader.h"
2020
#include "arcane/utils/IDataCompressor.h"
2121
#include "arcane/utils/IHashAlgorithm.h"
22+
#include "arcane/utils/Ref.h"
2223

2324
#include "arcane/core/IApplication.h"
2425
#include "arcane/core/IXmlDocumentHolder.h"
@@ -50,24 +51,13 @@ BasicGenericReader(IApplication* app, Int32 version, Ref<KeyValueTextReader> tex
5051
: TraceAccessor(app->traceMng())
5152
, m_application(app)
5253
, m_text_reader(text_reader)
53-
, m_rank(A_NULL_RANK)
5454
, m_version(version)
5555
{
5656
}
5757

5858
/*---------------------------------------------------------------------------*/
5959
/*---------------------------------------------------------------------------*/
6060

61-
BasicGenericReader::
62-
~BasicGenericReader()
63-
{
64-
for (const auto& x : m_variables_data_info)
65-
delete x.second;
66-
}
67-
68-
/*---------------------------------------------------------------------------*/
69-
/*---------------------------------------------------------------------------*/
70-
7161
void BasicGenericReader::
7262
initialize(const String& path, Int32 rank)
7363
{
@@ -143,7 +133,7 @@ initialize(const String& path, Int32 rank)
143133
for (Integer i = 0, is = variables_elem.size(); i < is; ++i) {
144134
XmlNode n = variables_elem[i];
145135
String var_full_name = n.attrValue("full-name");
146-
auto vdi = new VariableDataInfo(var_full_name, n);
136+
Ref<VariableDataInfo> vdi = makeRef(new VariableDataInfo(var_full_name, n));
147137
m_variables_data_info.insert(std::make_pair(var_full_name, vdi));
148138
}
149139

@@ -164,14 +154,14 @@ initialize(const String& path, Int32 rank)
164154
/*---------------------------------------------------------------------------*/
165155
/*---------------------------------------------------------------------------*/
166156

167-
VariableDataInfo* BasicGenericReader::
157+
Ref<VariableDataInfo> BasicGenericReader::
168158
_getVarInfo(const String& full_name)
169159
{
170160
VariableDataInfoMap::const_iterator ivar = m_variables_data_info.find(full_name);
171161
if (ivar == m_variables_data_info.end())
172162
ARCANE_THROW(ReaderWriterException,
173163
"Can not find own metadata infos for data var={0} rank={1}", full_name, m_rank);
174-
VariableDataInfo* vdi = ivar->second;
164+
Ref<VariableDataInfo> vdi = ivar->second;
175165
return vdi;
176166
}
177167

@@ -183,7 +173,7 @@ readData(const String& var_full_name, IData* data)
183173
{
184174
KeyValueTextReader* reader = m_text_reader.get();
185175
String vname = var_full_name;
186-
VariableDataInfo* vdi = _getVarInfo(vname);
176+
Ref<VariableDataInfo> vdi = _getVarInfo(vname);
187177
if (m_version < 3)
188178
reader->setFileOffset(vdi->fileOffset());
189179

@@ -354,18 +344,6 @@ initialize()
354344
/*---------------------------------------------------------------------------*/
355345
/*---------------------------------------------------------------------------*/
356346

357-
BasicReader::
358-
~BasicReader()
359-
{
360-
for (const auto& i : m_parallel_data_readers)
361-
delete i.second;
362-
for (const auto& r : m_global_readers)
363-
delete r;
364-
}
365-
366-
/*---------------------------------------------------------------------------*/
367-
/*---------------------------------------------------------------------------*/
368-
369347
void BasicReader::
370348
_directReadVal(VariableMetaData* varmd, IData* data)
371349
{
@@ -396,7 +374,7 @@ _directReadVal(VariableMetaData* varmd, IData* data)
396374
}
397375

398376
if (is_item_variable) {
399-
ParallelDataReader* parallel_data_reader = _getReader(varmd);
377+
Ref<ParallelDataReader> parallel_data_reader = _getReader(varmd);
400378

401379
Int64UniqueArray full_written_unique_ids;
402380
IData* full_written_data = nullptr;
@@ -439,7 +417,7 @@ _directReadVal(VariableMetaData* varmd, IData* data)
439417
/*---------------------------------------------------------------------------*/
440418
/*---------------------------------------------------------------------------*/
441419

442-
ParallelDataReader* BasicReader::
420+
Ref<ParallelDataReader> BasicReader::
443421
_getReader(VariableMetaData* varmd)
444422
{
445423
Int32 nb_to_read = m_nb_rank_to_read;
@@ -454,7 +432,7 @@ _getReader(VariableMetaData* varmd)
454432
return ix->second;
455433

456434
IParallelMng* pm = m_parallel_mng;
457-
auto reader = new ParallelDataReader(pm);
435+
Ref<ParallelDataReader> reader = makeRef(new ParallelDataReader(pm));
458436
{
459437
UniqueArray<SharedArray<Int64>> written_unique_ids(nb_to_read);
460438
Int64Array& wanted_unique_ids = reader->wantedUniqueIds();
@@ -609,7 +587,7 @@ _setRanksToRead()
609587
/*---------------------------------------------------------------------------*/
610588
/*---------------------------------------------------------------------------*/
611589

612-
IGenericReader* BasicReader::
590+
Ref<IGenericReader> BasicReader::
613591
_readOwnMetaDataAndCreateReader(Int32 rank)
614592
{
615593
String main_filename = _getBasicVariableFile(m_version, m_path, rank);
@@ -628,7 +606,7 @@ _readOwnMetaDataAndCreateReader(Int32 rank)
628606
}
629607
}
630608

631-
auto r = new BasicGenericReader(m_application, m_version, text_reader);
609+
auto r = makeRef<IGenericReader>(new BasicGenericReader(m_application, m_version, text_reader));
632610
r->initialize(m_path, rank);
633611
return r;
634612
}

Diff for: arcane/src/arcane/std/BasicWriter.cc

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* BasicWriter.cc (C) 2000-2023 */
8+
/* BasicWriter.cc (C) 2000-2024 */
99
/* */
1010
/* Ecriture simple pour les protections/reprises. */
1111
/*---------------------------------------------------------------------------*/
@@ -201,24 +201,13 @@ BasicWriter(IApplication* app, IParallelMng* pm, const String& path,
201201
eOpenMode open_mode, Integer version, bool want_parallel)
202202
: BasicReaderWriterCommon(app, pm, path, open_mode)
203203
, m_want_parallel(want_parallel)
204-
, m_is_gather(false)
205204
, m_version(version)
206205
{
207206
}
208207

209208
/*---------------------------------------------------------------------------*/
210209
/*---------------------------------------------------------------------------*/
211210

212-
BasicWriter::
213-
~BasicWriter()
214-
{
215-
for (const auto& i : m_parallel_data_writers)
216-
delete i.second;
217-
}
218-
219-
/*---------------------------------------------------------------------------*/
220-
/*---------------------------------------------------------------------------*/
221-
222211
void BasicWriter::
223212
initialize()
224213
{
@@ -264,14 +253,14 @@ initialize()
264253
/*---------------------------------------------------------------------------*/
265254
/*---------------------------------------------------------------------------*/
266255

267-
ParallelDataWriter* BasicWriter::
256+
Ref<ParallelDataWriter> BasicWriter::
268257
_getWriter(IVariable* var)
269258
{
270259
ItemGroup group = var->itemGroup();
271260
auto i = m_parallel_data_writers.find(group);
272261
if (i != m_parallel_data_writers.end())
273262
return i->second;
274-
ParallelDataWriter* writer = new ParallelDataWriter(m_parallel_mng);
263+
Ref<ParallelDataWriter> writer = makeRef<ParallelDataWriter>(new ParallelDataWriter(m_parallel_mng));
275264
writer->setGatherAll(m_is_gather);
276265
{
277266
Int64UniqueArray items_uid;
@@ -300,7 +289,7 @@ _directWriteVal(IVariable* var, IData* data)
300289
if (var->itemKind() != IK_Unknown) {
301290
ItemGroup group = var->itemGroup();
302291
if (m_want_parallel) {
303-
ParallelDataWriter* writer = _getWriter(var);
292+
Ref<ParallelDataWriter> writer = _getWriter(var);
304293
written_unique_ids = writer->sortedUniqueIds();
305294
allocated_write_data = writer->getSortedValues(data);
306295
write_data = allocated_write_data.get();
@@ -314,7 +303,7 @@ _directWriteVal(IVariable* var, IData* data)
314303
if (m_written_groups.find(group) == m_written_groups.end()) {
315304
info(5) << "WRITE GROUP " << group.name();
316305
IItemFamily* item_family = group.itemFamily();
317-
String gname = group.name();
306+
const String& gname = group.name();
318307
String group_full_name = item_family->fullName() + "_" + gname;
319308
m_global_writer->writeItemGroup(group_full_name, written_unique_ids, wanted_unique_ids.view());
320309
m_written_groups.insert(group);

Diff for: arcane/src/arcane/std/internal/BasicReaderWriter.h

+23-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* BasicReaderWriter.h (C) 2000-2023 */
8+
/* BasicReaderWriter.h (C) 2000-2024 */
99
/* */
1010
/* Lecture/Ecriture simple. */
1111
/*---------------------------------------------------------------------------*/
@@ -193,7 +193,6 @@ class BasicGenericReader
193193
// Si 'version==-1', alors cela sera déterminé lors de
194194
// l'initialisation.
195195
BasicGenericReader(IApplication* app, Int32 version, Ref<KeyValueTextReader> text_reader);
196-
~BasicGenericReader() override;
197196

198197
public:
199198

@@ -204,18 +203,18 @@ class BasicGenericReader
204203

205204
private:
206205

207-
using VariableDataInfoMap = std::map<String, VariableDataInfo*>;
206+
using VariableDataInfoMap = std::map<String, Ref<VariableDataInfo>>;
208207

209-
IApplication* m_application;
208+
IApplication* m_application = nullptr;
210209
Ref<KeyValueTextReader> m_text_reader;
211210
String m_path;
212-
Int32 m_rank;
213-
Int32 m_version;
211+
Int32 m_rank = A_NULL_RANK;
212+
Int32 m_version = -1;
214213
VariableDataInfoMap m_variables_data_info;
215214

216215
private:
217216

218-
VariableDataInfo* _getVarInfo(const String& full_name);
217+
Ref<VariableDataInfo> _getVarInfo(const String& full_name);
219218
};
220219

221220
/*---------------------------------------------------------------------------*/
@@ -228,7 +227,7 @@ class IGenericWriter
228227
{
229228
public:
230229

231-
virtual ~IGenericWriter() {}
230+
virtual ~IGenericWriter() = default;
232231

233232
public:
234233

@@ -335,7 +334,6 @@ class BasicWriter
335334

336335
BasicWriter(IApplication* app, IParallelMng* pm, const String& path,
337336
eOpenMode open_mode, Integer version, bool want_parallel);
338-
~BasicWriter() override;
339337

340338
public:
341339

@@ -355,26 +353,24 @@ class BasicWriter
355353

356354
private:
357355

358-
bool m_want_parallel;
359-
bool m_is_gather;
360-
Int32 m_version;
356+
bool m_want_parallel = false;
357+
bool m_is_gather = false;
358+
Int32 m_version = -1;
361359

362360
Ref<IDataCompressor> m_data_compressor;
363361
Ref<IHashAlgorithm> m_hash_algorithm;
364362
Ref<KeyValueTextWriter> m_text_writer;
365363

366-
std::map<ItemGroup, ParallelDataWriter*> m_parallel_data_writers;
364+
std::map<ItemGroup, Ref<ParallelDataWriter>> m_parallel_data_writers;
367365
std::set<ItemGroup> m_written_groups;
368366

369367
ScopedPtrT<IGenericWriter> m_global_writer;
370368

371369
private:
372370

373371
void _directWriteVal(IVariable* v, IData* data);
374-
void _writeVal(TextWriter* writer, VariableDataInfo* data_info,
375-
const ISerializedData* sdata);
376372

377-
ParallelDataWriter* _getWriter(IVariable* var);
373+
Ref<ParallelDataWriter> _getWriter(IVariable* var);
378374
};
379375

380376
/*---------------------------------------------------------------------------*/
@@ -405,7 +401,6 @@ class BasicReader
405401

406402
BasicReader(IApplication* app, IParallelMng* pm, Int32 forced_rank_to_read,
407403
const String& path, bool want_parallel);
408-
~BasicReader() override;
409404

410405
public:
411406

@@ -428,16 +423,16 @@ class BasicReader
428423

429424
private:
430425

431-
bool m_want_parallel;
432-
Integer m_nb_written_part;
433-
Int32 m_version;
426+
bool m_want_parallel = false;
427+
Integer m_nb_written_part = 0;
428+
Int32 m_version = -1;
434429

435-
Int32 m_first_rank_to_read;
436-
Int32 m_nb_rank_to_read;
437-
Int32 m_forced_rank_to_read;
430+
Int32 m_first_rank_to_read = -1;
431+
Int32 m_nb_rank_to_read = -1;
432+
Int32 m_forced_rank_to_read = -1;
438433

439-
std::map<String, ParallelDataReader*> m_parallel_data_readers;
440-
UniqueArray<IGenericReader*> m_global_readers;
434+
std::map<String, Ref<ParallelDataReader>> m_parallel_data_readers;
435+
UniqueArray<Ref<IGenericReader>> m_global_readers;
441436
IItemGroupFinder* m_item_group_finder;
442437
Ref<KeyValueTextReader> m_forced_rank_to_read_text_reader; //!< Lecteur pour le premier rang à lire.
443438
Ref<IDataCompressor> m_data_compressor;
@@ -446,9 +441,9 @@ class BasicReader
446441

447442
void _directReadVal(VariableMetaData* varmd, IData* data);
448443

449-
ParallelDataReader* _getReader(VariableMetaData* varmd);
444+
Ref<ParallelDataReader> _getReader(VariableMetaData* varmd);
450445
void _setRanksToRead();
451-
IGenericReader* _readOwnMetaDataAndCreateReader(Int32 rank);
446+
Ref<IGenericReader> _readOwnMetaDataAndCreateReader(Int32 rank);
452447
};
453448

454449
/*---------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)