Skip to content

Commit

Permalink
AMReX Algebra: AlgVector and SpMatrix
Browse files Browse the repository at this point in the history
This implements distributed vector and sparse matrix. Currently it only work
in serial on CPU.
  • Loading branch information
WeiqunZhang committed Dec 5, 2024
1 parent 2f10b41 commit ca53939
Show file tree
Hide file tree
Showing 12 changed files with 1,129 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Src/LinearSolvers/AMReX_AlgPartition.H
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
59 changes: 59 additions & 0 deletions Src/LinearSolvers/AMReX_AlgPartition.cpp
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;
}

}
Loading

0 comments on commit ca53939

Please sign in to comment.