Skip to content

Commit

Permalink
Remove more boilerplate code. (#90)
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <samuel@lanl.gov>
  • Loading branch information
samuelkgutierrez authored Mar 14, 2024
1 parent 5c1d82a commit d29c84d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 97 deletions.
71 changes: 26 additions & 45 deletions src/qvi-mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct qvi_mpi_group_s {
};

struct qvi_mpi_s {
int qvim_rc = QV_ERR_INTERNAL;
/** Task associated with this MPI process */
qvi_task_t *task = nullptr;
/** Node size */
Expand All @@ -50,7 +51,26 @@ struct qvi_mpi_s {
/** Node communicator */
MPI_Comm node_comm = MPI_COMM_NULL;
/** Group table (ID to internal structure mapping) */
qvi_mpi_group_tab_t *group_tab = nullptr;
qvi_mpi_group_tab_t group_tab;
/** Constructor */
qvi_mpi_s(void)
{
qvim_rc = qvi_task_new(&task);
}
/** Destructor */
~qvi_mpi_s(void)
{
for (auto &i : group_tab) {
auto &mpi_comm = i.second.mpi_comm;
if (mpi_comm != MPI_COMM_NULL) {
MPI_Comm_free(&mpi_comm);
}
}
if (world_comm != MPI_COMM_NULL) {
MPI_Comm_free(&world_comm);
}
qvi_task_free(&task);
}
};

/**
Expand Down Expand Up @@ -203,61 +223,22 @@ group_add(
if (rc != QV_SUCCESS) return rc;
}
group->tabid = gtid;
mpi->group_tab->insert({gtid, *group});
mpi->group_tab.insert({gtid, *group});
return QV_SUCCESS;
}

int
qvi_mpi_new(
qvi_mpi_t **mpi
) {
int rc = QV_SUCCESS;

qvi_mpi_t *impi = qvi_new qvi_mpi_t();
if (!impi) {
rc = QV_ERR_OOR;
goto out;
}
// Task
rc = qvi_task_new(&impi->task);
if (rc != QV_SUCCESS) goto out;
// Groups
impi->group_tab = qvi_new qvi_mpi_group_tab_t();
if (!impi->group_tab) {
rc = QV_ERR_OOR;
}
out:
if (rc != QV_SUCCESS) {
qvi_mpi_free(&impi);
}
*mpi = impi;
return rc;
return qvi_new_rc(mpi);
}

void
qvi_mpi_free(
qvi_mpi_t **mpi
) {
if (!mpi) return;
qvi_mpi_t *impi = *mpi;
if (!impi) goto out;
if (impi->group_tab) {
for (auto &i : *impi->group_tab) {
auto &mpi_comm = i.second.mpi_comm;
if (mpi_comm != MPI_COMM_NULL) {
MPI_Comm_free(&mpi_comm);
}
}
delete impi->group_tab;
impi->group_tab = nullptr;
}
if (impi->world_comm != MPI_COMM_NULL) {
MPI_Comm_free(&impi->world_comm);
}
qvi_task_free(&impi->task);
delete impi;
out:
*mpi = nullptr;
qvi_delete(mpi);
}

qvi_task_t *
Expand Down Expand Up @@ -481,8 +462,8 @@ qvi_mpi_group_lookup_by_id(
qvi_mpi_group_id_t id,
qvi_mpi_group_t *group
) {
auto got = mpi->group_tab->find(id);
if (got == mpi->group_tab->end()) {
auto got = mpi->group_tab.find(id);
if (got == mpi->group_tab.end()) {
return QV_ERR_NOT_FOUND;
}
cp_mpi_group(&got->second, group);
Expand Down
75 changes: 23 additions & 52 deletions src/qvi-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/

#include "qvi-common.h"

#include "qvi-process.h"
#include "qvi-utils.h"
#include "qvi-group.h"

// Type definitions.
Expand All @@ -24,19 +24,35 @@ using qvi_process_group_tab_t = std::unordered_map<
>;

struct qvi_process_group_s {
int qvim_rc = QV_SUCCESS;
/** ID used for table lookups */
qvi_process_group_id_t tabid = 0;
/** ID (rank) in group */
int id = 0;
/** Size of group */
int size = 0;
/** Constructor */
qvi_process_group_s(void) = default;
/** Destructor */
~qvi_process_group_s(void) = default;
};

struct qvi_process_s {
int qvim_rc = QV_ERR_INTERNAL;
/** Task associated with this process */
qvi_task_t *task = nullptr;
/** Maintains the next available group ID value */
qvi_process_group_tab_t *group_tab = nullptr;
qvi_process_group_tab_t group_tab;
/** Constructor */
qvi_process_s(void)
{
qvim_rc = qvi_task_new(&task);
}
/** Destructor */
~qvi_process_s(void)
{
qvi_task_free(&task);
}
};

/**
Expand All @@ -54,44 +70,14 @@ int
qvi_process_new(
qvi_process_t **proc
) {
int rc = QV_SUCCESS;

qvi_process_t *iproc = qvi_new qvi_process_t();
if (!iproc) {
rc = QV_ERR_OOR;
goto out;
}
// Task
rc = qvi_task_new(&iproc->task);
if (rc != QV_SUCCESS) goto out;
// Groups
iproc->group_tab = qvi_new qvi_process_group_tab_t();
if (!iproc->group_tab) {
rc = QV_ERR_OOR;
}
out:
if (rc != QV_SUCCESS) {
qvi_process_free(&iproc);
}
*proc = iproc;
return rc;
return qvi_new_rc(proc);
}

void
qvi_process_free(
qvi_process_t **proc
) {
if (!proc) return;
qvi_process_t *iproc = *proc;
if (!iproc) goto out;
if (iproc->group_tab) {
delete iproc->group_tab;
iproc->group_tab = nullptr;
}
qvi_task_free(&iproc->task);
delete iproc;
out:
*proc = nullptr;
return qvi_delete(proc);
}

/**
Expand Down Expand Up @@ -134,29 +120,14 @@ int
qvi_process_group_new(
qvi_process_group_t **procgrp
) {
int rc = QV_SUCCESS;

qvi_process_group_t *iprocgrp = qvi_new qvi_process_group_t();
if (!iprocgrp) {
rc = QV_ERR_OOR;
}
if (rc != QV_SUCCESS) {
qvi_process_group_free(&iprocgrp);
}
*procgrp = iprocgrp;
return rc;
return qvi_new_rc(procgrp);
}

void
qvi_process_group_free(
qvi_process_group_t **procgrp
) {
if (!procgrp) return;
qvi_process_group_t *iprocgrp = *procgrp;
if (!iprocgrp) goto out;
delete iprocgrp;
out:
*procgrp = nullptr;
qvi_delete(procgrp);
}

int
Expand All @@ -177,7 +148,7 @@ qvi_process_group_create(
igroup->id = 0;
igroup->size = 1;

proc->group_tab->insert({gtid, *igroup});
proc->group_tab.insert({gtid, *igroup});
out:
if (rc != QV_SUCCESS) {
qvi_process_group_free(&igroup);
Expand Down

0 comments on commit d29c84d

Please sign in to comment.