Skip to content

Commit

Permalink
Simplify more code. (#149)
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <[email protected]>
  • Loading branch information
samuelkgutierrez authored May 22, 2024
1 parent 4f82f0f commit 65b6827
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 158 deletions.
10 changes: 2 additions & 8 deletions src/qvi-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ int
qvi_context_new(
qv_context_t **ctx
) {
int rc = QV_SUCCESS;
qv_context_t *ictx = new qv_context_t();

qv_context_t *ictx = qvi_new qv_context_t();
if (!ictx) {
rc = QV_ERR_OOR;
goto out;
}

rc = qvi_rmi_client_new(&ictx->rmi);
int rc = qvi_rmi_client_new(&ictx->rmi);
if (rc != QV_SUCCESS) goto out;

rc = qvi_bind_stack_new(&ictx->bind_stack);
Expand Down
20 changes: 4 additions & 16 deletions src/qvi-group-mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,9 @@ int
qvi_group_mpi_s::self(
qvi_group_t **child
) {
int rc = QV_SUCCESS;

qvi_group_mpi_t *ichild = qvi_new qvi_group_mpi_t();
if (!ichild) {
rc = QV_ERR_OOR;
goto out;
}
qvi_group_mpi_t *ichild = new qvi_group_mpi_t();
// Initialize the child with the parent's MPI instance.
rc = ichild->initialize(mpi);
int rc = ichild->initialize(mpi);
if (rc != QV_SUCCESS) goto out;
// Create the underlying group using MPI_COMM_SELF.
rc = qvi_mpi_group_create_from_mpi_comm(
Expand All @@ -95,15 +89,9 @@ qvi_group_mpi_s::split(
int key,
qvi_group_t **child
) {
int rc = QV_SUCCESS;

qvi_group_mpi_t *ichild = qvi_new qvi_group_mpi_t();
if (!ichild) {
rc = QV_ERR_OOR;
goto out;
}
qvi_group_mpi_t *ichild = new qvi_group_mpi_t();
// Initialize the child with the parent's MPI instance.
rc = ichild->initialize(mpi);
int rc = ichild->initialize(mpi);
if (rc != QV_SUCCESS) goto out;
// Split this group using MPI.
rc = qvi_mpi_group_create_from_split(
Expand Down
10 changes: 2 additions & 8 deletions src/qvi-group-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ int
qvi_group_process_s::self(
qvi_group_t **child
) {
int rc = QV_SUCCESS;

qvi_group_process_t *ichild = qvi_new qvi_group_process_t();
if (!ichild) {
rc = QV_ERR_OOR;
goto out;
}
qvi_group_process_t *ichild = new qvi_group_process_t();
// Initialize the child with the parent's process instance.
rc = ichild->initialize(proc);
int rc = ichild->initialize(proc);
if (rc != QV_SUCCESS) goto out;
// Because this is in the context of a process, the concept of splitting
// doesn't really apply here, so just create another process group.
Expand Down
86 changes: 23 additions & 63 deletions src/qvi-mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -663,21 +663,17 @@ qvi_mpi_group_gather_bbuffs(
const int group_size = group->size;

int rc = QV_SUCCESS, mpirc = MPI_SUCCESS;
int *rxcounts = nullptr, *displs = nullptr;
byte_t *allbytes = nullptr;
std::vector<int> rxcounts, displs;
std::vector<byte_t> allbytes;
qvi_bbuff_t **bbuffs = nullptr;

if (group_id == root) {
rxcounts = qvi_new int[group_size]();
if (!rxcounts) {
rc = QV_ERR_OOR;
goto out;
}
rxcounts.resize(group_size);
}
// Figure out now much data are sent by each participant.
mpirc = MPI_Gather(
&send_count, 1, MPI_INT,
rxcounts, 1, MPI_INT,
rxcounts.data(), 1, MPI_INT,
root, group->mpi_comm
);
if (mpirc != MPI_SUCCESS) {
Expand All @@ -686,28 +682,19 @@ qvi_mpi_group_gather_bbuffs(
}
// Root sets up relevant Gatherv data structures.
if (group_id == root) {
displs = qvi_new int[group_size]();
if (!displs) {
rc = QV_ERR_OOR;
goto out;
};
displs.resize(group_size);

int total_bytes = 0;
for (int i = 0; i < group_size; ++i) {
displs[i] = total_bytes;
total_bytes += rxcounts[i];
}

allbytes = qvi_new byte_t[total_bytes]();
if (!allbytes) {
rc = QV_ERR_OOR;
goto out;
};
allbytes.resize(total_bytes);
}

mpirc = MPI_Gatherv(
qvi_bbuff_data(txbuff), send_count, MPI_UINT8_T,
allbytes, rxcounts, displs, MPI_UINT8_T,
allbytes.data(), rxcounts.data(), displs.data(), MPI_UINT8_T,
root, group->mpi_comm
);
if (mpirc != MPI_SUCCESS) {
Expand All @@ -717,12 +704,9 @@ qvi_mpi_group_gather_bbuffs(
// Root creates new buffers from data gathered from each participant.
if (group_id == root) {
// Zero initialize array of pointers to nullptr.
bbuffs = qvi_new qvi_bbuff_t*[group_size]();
if (!bbuffs) {
rc = QV_ERR_OOR;
goto out;
}
byte_t *bytepos = allbytes;
bbuffs = new qvi_bbuff_t*[group_size]();

byte_t *bytepos = allbytes.data();
for (int i = 0; i < group_size; ++i) {
rc = qvi_bbuff_new(&bbuffs[i]);
if (rc != QV_SUCCESS) break;
Expand All @@ -732,9 +716,6 @@ qvi_mpi_group_gather_bbuffs(
}
}
out:
delete[] rxcounts;
delete[] displs;
delete[] allbytes;
if (rc != QV_SUCCESS) {
if (bbuffs) {
for (int i = 0; i < group_size; ++i) {
Expand All @@ -760,44 +741,32 @@ qvi_mpi_group_scatter_bbuffs(
const int group_id = group->id;

int rc = QV_SUCCESS, mpirc = MPI_SUCCESS, rxcount = 0;
int *txcounts = nullptr, *displs = nullptr, total_bytes = 0;
byte_t *mybytes = nullptr, *txbytes = nullptr;
int total_bytes = 0;
std::vector<int> txcounts, displs;
std::vector<byte_t> mybytes, txbytes;
qvi_bbuff_t *mybbuff = nullptr;
// Root sets up relevant Scatterv data structures.
if (group_id == root) {
txcounts = qvi_new int[group_size]();
if (!txcounts) {
rc = QV_ERR_OOR;
goto out;
}

displs = qvi_new int[group_size]();
if (!displs) {
rc = QV_ERR_OOR;
goto out;
};
txcounts.resize(group_size);
displs.resize(group_size);

for (int i = 0; i < group_size; ++i) {
txcounts[i] = (int)qvi_bbuff_size(txbuffs[i]);
displs[i] = total_bytes;
total_bytes += txcounts[i];
}
// A flattened buffer containing all the relevant information.
txbytes = qvi_new byte_t[total_bytes]();
if (!txbytes) {
rc = QV_ERR_OOR;
goto out;
};
txbytes.resize(total_bytes);
// Copy buffer data into flattened buffer.
byte_t *bytepos = txbytes;
byte_t *bytepos = txbytes.data();
for (int i = 0; i < group_size; ++i) {
memmove(bytepos, qvi_bbuff_data(txbuffs[i]), txcounts[i]);
bytepos += txcounts[i];
}
}
// Scatter buffer sizes.
mpirc = MPI_Scatter(
txcounts, 1, MPI_INT,
txcounts.data(), 1, MPI_INT,
&rxcount, 1, MPI_INT,
root, group->mpi_comm
);
Expand All @@ -806,16 +775,11 @@ qvi_mpi_group_scatter_bbuffs(
goto out;
}
// Everyone allocates a buffer for their data.
mybytes = qvi_new byte_t[rxcount]();
if (!mybytes) {
rc = QV_ERR_OOR;
goto out;
};
mybytes.resize(rxcount);

mpirc = MPI_Scatterv(
txbytes, txcounts, displs, MPI_UINT8_T,
mybytes, rxcount, MPI_UINT8_T,
root, group->mpi_comm
txbytes.data(), txcounts.data(), displs.data(), MPI_UINT8_T,
mybytes.data(), rxcount, MPI_UINT8_T, root, group->mpi_comm
);
if (mpirc != MPI_SUCCESS) {
rc = QV_ERR_MPI;
Expand All @@ -824,12 +788,8 @@ qvi_mpi_group_scatter_bbuffs(
// Everyone creates new buffers from data received from root.
rc = qvi_bbuff_new(&mybbuff);
if (rc != QV_SUCCESS) goto out;
rc = qvi_bbuff_append(mybbuff, mybytes, rxcount);
rc = qvi_bbuff_append(mybbuff, mybytes.data(), rxcount);
out:
delete[] txcounts;
delete[] displs;
delete[] txbytes;
delete[] mybytes;
if (rc != QV_SUCCESS) {
qvi_bbuff_free(&mybbuff);
}
Expand All @@ -842,7 +802,7 @@ qvi_mpi_group_comm_dup(
qvi_mpi_group_t *group,
MPI_Comm *comm
) {
int rc = MPI_Comm_dup(group->mpi_comm, comm);
const int rc = MPI_Comm_dup(group->mpi_comm, comm);
if (rc != MPI_SUCCESS) return QV_ERR_MPI;
return QV_SUCCESS;
}
Expand Down
28 changes: 7 additions & 21 deletions src/qvi-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,38 +187,24 @@ qvi_process_group_gather_bbuffs(
) {
const int group_size = qvi_process_group_size(group);
// Make sure that we are dealing with a valid process group.
assert(root == 0 && group_size == 1);
// If not, this is an internal development error, so abort.
if (root != 0 || group_size != 1) {
return QV_ERR_INTERNAL;
qvi_abort();
}

int rc = QV_SUCCESS;
int *rxcounts = nullptr;
byte_t *bytepos = nullptr;
std::vector<size_t> rxcounts = {qvi_bbuff_size(txbuff)};
// Zero initialize array of pointers to nullptr.
qvi_bbuff_t **bbuffs = qvi_new qvi_bbuff_t *[group_size]();
if (!bbuffs) {
rc = QV_ERR_OOR;
goto out;
}

rxcounts = qvi_new int[group_size]();
if (!rxcounts) {
rc = QV_ERR_OOR;
goto out;
}
rxcounts[0] = qvi_bbuff_size(txbuff);
qvi_bbuff_t **bbuffs = new qvi_bbuff_t *[group_size]();

bytepos = (byte_t *)qvi_bbuff_data(txbuff);
byte_t *bytepos = (byte_t *)qvi_bbuff_data(txbuff);
for (int i = 0; i < group_size; ++i) {
rc = qvi_bbuff_new(&bbuffs[i]);
if (rc != QV_SUCCESS) break;
rc = qvi_bbuff_append(bbuffs[i], bytepos, rxcounts[i]);
if (rc != QV_SUCCESS) break;
bytepos += rxcounts[i];
}
out:
delete[] rxcounts;
if (rc != QV_SUCCESS) {
if (bbuffs) {
for (int i = 0; i < group_size; ++i) {
Expand All @@ -242,9 +228,9 @@ qvi_process_group_scatter_bbuffs(
) {
const int group_size = qvi_process_group_size(group);
// Make sure that we are dealing with a valid process group.
assert(root == 0 && group_size == 1);
// If not, this is an internal development error, so abort.
if (root != 0 || group_size != 1) {
return QV_ERR_INTERNAL;
qvi_abort();
}
// There should always be only one at the root (us).
qvi_bbuff_t *inbuff = txbuffs[root];
Expand Down
15 changes: 3 additions & 12 deletions src/qvi-rmi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,7 @@ qvi_rmi_server_new(
int rc = QV_SUCCESS;
cstr_t ers = nullptr;

qvi_rmi_server_t *iserver = qvi_new qvi_rmi_server_t();
if (!iserver) {
ers = "memory allocation failed";
rc = QV_ERR_OOR;
goto out;
}
qvi_rmi_server_t *iserver = new qvi_rmi_server_t();

iserver->zctx = zmq_ctx_new();
if (!iserver->zctx) {
Expand Down Expand Up @@ -897,19 +892,15 @@ qvi_rmi_server_start(
return qvrc;
}

// TODO(skg) Use new method.
int
qvi_rmi_client_new(
qvi_rmi_client_t **client
) {
int rc = QV_SUCCESS;
cstr_t ers = nullptr;

qvi_rmi_client_t *icli = qvi_new qvi_rmi_client_t();
if (!icli) {
ers = "memory allocation failed";
rc = QV_ERR_OOR;
goto out;
}
qvi_rmi_client_t *icli = new qvi_rmi_client_t();

// Remember clients own the hwloc data, unlike the server.
rc = qvi_hwloc_new(&icli->config.hwloc);
Expand Down
3 changes: 1 addition & 2 deletions src/qvi-scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,7 @@ qvi_scope_ksplit(
if (rc != QV_SUCCESS) return rc;

// Now populate the children.
qv_scope_t **ikchildren = qvi_new qv_scope_t*[group_size];
if (!ikchildren) return QV_ERR_OOR;
qv_scope_t **ikchildren = new qv_scope_t*[group_size];

for (uint_t i = 0; i < group_size; ++i) {
// Split off from our parent group. This call is usually called from a
Expand Down
17 changes: 3 additions & 14 deletions src/qvi-zgroup-mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ qvi_zgroup_mpi_s::group_create_intrinsic(
) {
int rc = QV_SUCCESS;

qvi_group_mpi_t *igroup = qvi_new qvi_group_mpi_t();
if (!igroup) {
rc = QV_ERR_OOR;
goto out;
}
qvi_group_mpi_t *igroup = new qvi_group_mpi_t();

rc = igroup->initialize(mpi);
if (rc != QV_SUCCESS) goto out;
Expand Down Expand Up @@ -98,16 +94,9 @@ int
qvi_zgroup_mpi_new(
qvi_zgroup_mpi_t **zgroup
) {
int rc = QV_SUCCESS;
qvi_zgroup_mpi_t *izgroup = new qvi_zgroup_mpi_t();

qvi_zgroup_mpi_t *izgroup = qvi_new qvi_zgroup_mpi_t();
if (!izgroup) {
rc = QV_ERR_OOR;
goto out;
}

rc = izgroup->create();
out:
const int rc = izgroup->create();
if (rc != QV_SUCCESS) {
qvi_zgroup_mpi_free(&izgroup);
}
Expand Down
Loading

0 comments on commit 65b6827

Please sign in to comment.