forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AMReX Algebra: AlgVector and SpMatrix
This implements distributed vector and sparse matrix. Currently it only work in serial on CPU.
- Loading branch information
1 parent
2f10b41
commit ca53939
Showing
12 changed files
with
1,129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef AMREX_ALG_PARTITION_H_ | ||
#define AMREX_ALG_PARTITION_H_ | ||
#include <AMReX_Config.H> | ||
|
||
#include <AMReX_INT.H> | ||
#include <AMReX_ParallelDescriptor.H> | ||
#include <AMReX_Vector.H> | ||
|
||
#include <memory> | ||
|
||
namespace amrex { | ||
|
||
class AlgPartition | ||
{ | ||
public: | ||
AlgPartition (); | ||
explicit AlgPartition (Long global_size); | ||
explicit AlgPartition (Vector<Long> const& rows); | ||
explicit AlgPartition (Vector<Long>&& rows) noexcept; | ||
|
||
void define (Long global_size); | ||
void define (Vector<Long> const& rows); | ||
void define (Vector<Long>&& rows); | ||
|
||
Long operator[] (int i) const { return m_ref->m_row[i]; } | ||
Long numGlobalRows () const { return m_ref->m_row.back(); } | ||
|
||
private: | ||
struct Ref | ||
{ | ||
friend class AlgPartition; | ||
Ref () = default; | ||
explicit Ref (Long global_size); | ||
Ref (Vector<Long> const& rows) : m_row(rows) { | ||
AMREX_ASSERT(m_row.size() == ParallelDescriptor::NProcs()+1); | ||
} | ||
Ref (Vector<Long>&& rows) : m_row(std::move(rows)) { | ||
AMREX_ASSERT(m_row.size() == ParallelDescriptor::NProcs()); | ||
} | ||
void define (Long global_size); | ||
Vector<Long> m_row; // size: nprocs + 1 | ||
}; | ||
|
||
std::shared_ptr<Ref> m_ref; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <AMReX_AlgPartition.H> | ||
|
||
namespace amrex { | ||
|
||
AlgPartition::AlgPartition () | ||
: m_ref(std::make_shared<Ref>()) | ||
{} | ||
|
||
AlgPartition::AlgPartition (Long global_size) | ||
: m_ref(std::make_shared<Ref>(global_size)) | ||
{} | ||
|
||
AlgPartition::AlgPartition (Vector<Long> const& rows) | ||
: m_ref(std::make_shared<Ref>(rows)) | ||
{} | ||
|
||
AlgPartition::AlgPartition (Vector<Long>&& rows) noexcept | ||
: m_ref(std::make_shared<Ref>(std::move(rows))) | ||
{} | ||
|
||
void AlgPartition::define (Long global_size) | ||
{ | ||
m_ref->define(global_size); | ||
} | ||
|
||
void AlgPartition::define (Vector<Long> const& rows) | ||
{ | ||
m_ref->m_row = rows; | ||
AMREX_ASSERT(m_ref->m_row.size() == ParallelDescriptor::NProcs()); | ||
} | ||
|
||
void AlgPartition::define (Vector<Long>&& rows) | ||
{ | ||
m_ref->m_row = std::move(rows); | ||
AMREX_ASSERT(m_ref->m_row.size() == ParallelDescriptor::NProcs()); | ||
} | ||
|
||
AlgPartition::Ref::Ref (Long global_size) | ||
{ | ||
define(global_size); | ||
} | ||
|
||
void AlgPartition::Ref::define (Long global_size) | ||
{ | ||
auto nprocs = Long(ParallelDescriptor::NProcs()); | ||
Long sz = global_size / nprocs; | ||
Long extra = global_size - sz*nprocs; | ||
m_row.resize(nprocs+1); | ||
for (Long i = 0; i < nprocs; ++i) { | ||
if (i < extra) { | ||
m_row[i] = i*(sz+1); | ||
} else { | ||
m_row[i] = i*sz + extra; | ||
} | ||
} | ||
m_row[nprocs] = global_size; | ||
} | ||
|
||
} |
Oops, something went wrong.