sg-ode
Data Structures | Typedefs | Functions | Variables
vector.h File Reference

A generic interface for elementwise and aggregate operations on vectors. More...

#include <stddef.h>
#include "extern.h"
#include "inline_begin.h"
#include "restrict_begin.h"
#include "restrict_end.h"
#include "inline_end.h"

Go to the source code of this file.

Data Structures

struct  SgVectorDriverVt
 The minimal set of operations that a vector driver must support. More...
 
struct  SgVectorDriver
 A vector driver. More...
 

Typedefs

typedef void SgVector
 A vector is an opaque data type containing a finite number of doubles. More...
 
typedef void SgVectorAccum
 An accumulator is an array of some kind. More...
 
typedef int SgVectorAccumType
 The type of an accumulator. More...
 
typedef void SgVectorOperation(void *f_ctx, SgVectorAccum *accum, const SgVectorAccum *val, size_t offset, double **data, size_t num_elems)
 An operation suitable for use in SgVectorDriverVt.operate. More...
 
typedef void SgVectorDriverBase
 Represents an opaque vector driver. More...
 

Functions

SgVectorsg_vector_try_new (struct SgVectorDriver drv)
 Creates a vector, returning NULL if it fails. More...
 
SgVectorsg_vector_new (struct SgVectorDriver drv)
 Creates a vector. More...
 
void sg_vector_del (struct SgVectorDriver drv, SgVector *vector)
 Destroys the vector. More...
 
size_t sg_vector_len (struct SgVectorDriver drv)
 Gets the length of any vector created by this vector driver. More...
 
void sg_vector_operate (struct SgVectorDriver drv, SgVectorAccum *accum, SgVectorAccumType accum_type, SgVectorOperation *f, void *f_ctx, size_t offset, SgVector **vectors, size_t num_vectors)
 Perform a generic operation on some vectors. More...
 
void sg_vector_fill (struct SgVectorDriver drv, double value, SgVector *vector)
 Set every element of a vector to the same value. More...
 
void sg_vector_copy (struct SgVectorDriver drv, const SgVector *src, SgVector *dest)
 Copy the contents of a vector. More...
 
void sg_vector_neg_assign (struct SgVectorDriver drv, SgVector *z)
 
void sg_vector_neg (struct SgVectorDriver drv, const SgVector *x, SgVector *z)
 
void sg_vector_scale_assign (struct SgVectorDriver drv, double alpha, SgVector *z)
 In-place scalar multiplication of a vector. More...
 
void sg_vector_scale (struct SgVectorDriver drv, double alpha, const SgVector *x, SgVector *z)
 Calculate scalar multiplication of a vector. More...
 
void sg_vector_linear_assign (struct SgVectorDriver drv, double alpha, double beta, const SgVector *y, SgVector *z)
 Calculate z ← α z + β y. More...
 
void sg_vector_linear (struct SgVectorDriver drv, double alpha, const SgVector *x, double beta, const SgVector *y, SgVector *z)
 Calculate the linear combination of two vectors. More...
 
double sg_vector_sum (struct SgVectorDriver drv, const SgVector *vector)
 Sums the values of a vector. More...
 

Variables

SgVectorOperation sg_vector_fill_operation
 
SgVectorOperation sg_vector_copy_operation
 
SgVectorOperation sg_vector_neg_assign_operation
 
SgVectorOperation sg_vector_neg_operation
 
SgVectorOperation sg_vector_scale_assign_operation
 
SgVectorOperation sg_vector_scale_operation
 
SgVectorOperation sg_vector_linear_assign_operation
 
SgVectorOperation sg_vector_linear_operation
 
SgVectorOperation sg_vector_sum_operation
 The underlying operation used by sg_vector_sum. More...
 
struct SgVectorDriverVt SG_BASIC_VECTOR_DRIVER_VT
 

Detailed Description

A generic interface for elementwise and aggregate operations on vectors.

Typedef Documentation

◆ SgVector

typedef void SgVector

A vector is an opaque data type containing a finite number of doubles.

◆ SgVectorAccum

typedef void SgVectorAccum

An accumulator is an array of some kind.

◆ SgVectorAccumType

typedef int SgVectorAccumType

The type of an accumulator.

  • A positive integer indicates that the accumulator is an array of unsigned char, with length given by the absolute value of the type.
  • A negative integer indicates that the accumulator is an array of double, with length given by the absolute value of the type.
  • Zero indicates that the accumulator is empty and should be ignored entirely (the Accum pointer may not even be valid).

It is important to specify the correct accumulator type, as the SgVectorDriver implementation may use this information to transmit the accumulator across different processes.

◆ SgVectorDriverBase

typedef void SgVectorDriverBase

Represents an opaque vector driver.

◆ SgVectorOperation

typedef void SgVectorOperation(void *f_ctx, SgVectorAccum *accum, const SgVectorAccum *val, size_t offset, double **data, size_t num_elems)

An operation suitable for use in SgVectorDriverVt.operate.

Parameters
[in,out]f_ctxThe same f_ctx pointer provided to operate.
[in,out]accumThe accumulated value so far.
[in]valA value that needs to be merged into accum.
[in]offsetOffset of this slice relative to the beginning.
[in,out]dataPartial slices of the vectors.
[in]num_elemsNumber of elements in each slice.

The function must update accum to the monoidal summation of:

  • accum,
  • val, and
  • all values derived from the subvector elements (starting at index 0 and ending at num_elems - 1)

The function may also perform an element-wise operation on the given subvectors. It can modify the subvectors, in which case the changes will be reflected on the original vectors.

For a given value of f_ctx, function f must be thread-safe. That is, it must not access global variables without synchronization.

Function Documentation

◆ sg_vector_copy()

void sg_vector_copy ( struct SgVectorDriver  drv,
const SgVector src,
SgVector dest 
)

Copy the contents of a vector.

◆ sg_vector_del()

void sg_vector_del ( struct SgVectorDriver  drv,
SgVector vector 
)

Destroys the vector.

This is a no-op if the vector is null.

◆ sg_vector_fill()

void sg_vector_fill ( struct SgVectorDriver  drv,
double  value,
SgVector vector 
)

Set every element of a vector to the same value.

◆ sg_vector_len()

size_t sg_vector_len ( struct SgVectorDriver  drv)

Gets the length of any vector created by this vector driver.

◆ sg_vector_linear()

void sg_vector_linear ( struct SgVectorDriver  drv,
double  alpha,
const SgVector x,
double  beta,
const SgVector y,
SgVector z 
)

Calculate the linear combination of two vectors.

Either or both input vectors may completely overlap with the output vector. Partial overlaps are not allowed.

◆ sg_vector_linear_assign()

void sg_vector_linear_assign ( struct SgVectorDriver  drv,
double  alpha,
double  beta,
const SgVector y,
SgVector z 
)

Calculate z ← α z + β y.

The input vector y may completely overlap with the output vector z. Partial overlaps are not allowed.

◆ sg_vector_new()

SgVector* sg_vector_new ( struct SgVectorDriver  drv)

Creates a vector.

◆ sg_vector_operate()

void sg_vector_operate ( struct SgVectorDriver  drv,
SgVectorAccum accum,
SgVectorAccumType  accum_type,
SgVectorOperation f,
void *  f_ctx,
size_t  offset,
SgVector **  vectors,
size_t  num_vectors 
)

Perform a generic operation on some vectors.

See SgVectorDriverVt.operate.

◆ sg_vector_scale()

void sg_vector_scale ( struct SgVectorDriver  drv,
double  alpha,
const SgVector x,
SgVector z 
)

Calculate scalar multiplication of a vector.

The input vectors may completely overlap with the output vector. Partial overlaps are not allowed.

◆ sg_vector_scale_assign()

void sg_vector_scale_assign ( struct SgVectorDriver  drv,
double  alpha,
SgVector z 
)

In-place scalar multiplication of a vector.

◆ sg_vector_sum()

double sg_vector_sum ( struct SgVectorDriver  drv,
const SgVector vector 
)

Sums the values of a vector.

◆ sg_vector_try_new()

SgVector* sg_vector_try_new ( struct SgVectorDriver  drv)

Creates a vector, returning NULL if it fails.

Variable Documentation

◆ sg_vector_sum_operation

SgVectorOperation sg_vector_sum_operation

The underlying operation used by sg_vector_sum.