Permalink
Browse files

Moved yale storage functions to a yale_storage namespace, added docum…

…entation for yale stuff.
  • Loading branch information...
1 parent ec279ae commit e25083f1ee1642f07e072c2da99d39f219277216 @mohawkjohn mohawkjohn committed Aug 21, 2012
Showing with 615 additions and 533 deletions.
  1. +1 −1 ext/nmatrix/nmatrix.h
  2. +102 −95 ext/nmatrix/storage/storage.cpp
  3. +30 −26 ext/nmatrix/storage/storage.h
  4. +359 −294 ext/nmatrix/storage/yale.cpp
  5. +123 −117 ext/nmatrix/storage/yale.h
View
@@ -305,9 +305,9 @@ NM_DEF_STRUCT_POST(NMATRIX); // };
(rb_obj_is_kind_of(obj, cNVector) == Qtrue)
-#ifdef __cplusplus
typedef VALUE (*METHOD)(...);
+#ifdef __cplusplus
//}; // end of namespace nm
#endif
@@ -336,144 +336,151 @@ STORAGE* list_storage_from_yale(const STORAGE* right, dtype_t l_dtype) {
}
-/*
- * Creation of yale storage from dense storage.
- */
-template <typename LDType, typename RDType, typename LIType>
-YALE_STORAGE* yale_storage_from_dense_template(const DENSE_STORAGE* rhs, dtype_t l_dtype) {
- LIType pos = 0, ndnz = 0;
+namespace yale_storage { // FIXME: Move to yale.cpp
+ /*
+ * Creation of yale storage from dense storage.
+ */
+ template <typename LDType, typename RDType, typename LIType>
+ YALE_STORAGE* create_from_dense_storage(const DENSE_STORAGE* rhs, dtype_t l_dtype) {
+ LIType pos = 0, ndnz = 0;
- RDType R_ZERO; // need zero for easier comparisons
- if (rhs->dtype == RUBYOBJ) R_ZERO = INT2FIX(0);
- else R_ZERO = 0;
+ RDType R_ZERO; // need zero for easier comparisons
+ if (rhs->dtype == RUBYOBJ) R_ZERO = INT2FIX(0);
+ else R_ZERO = 0;
- if (rhs->rank != 2) rb_raise(nm_eStorageTypeError, "can only convert matrices of rank 2 to yale");
+ if (rhs->rank != 2) rb_raise(nm_eStorageTypeError, "can only convert matrices of rank 2 to yale");
- RDType* rhs_elements = reinterpret_cast<RDType*>(rhs->elements);
+ RDType* rhs_elements = reinterpret_cast<RDType*>(rhs->elements);
- // First, count the non-diagonal nonzeros
- for (size_t i = rhs->shape[0]; i-- > 0;) {
- for (size_t j = rhs->shape[1]; j-- > 0;) {
- if (i != j && rhs_elements[pos] != R_ZERO) ++ndnz;
+ // First, count the non-diagonal nonzeros
+ for (size_t i = rhs->shape[0]; i-- > 0;) {
+ for (size_t j = rhs->shape[1]; j-- > 0;) {
+ if (i != j && rhs_elements[pos] != R_ZERO) ++ndnz;
- // move forward 1 position in dense matrix elements array
- ++pos;
+ // move forward 1 position in dense matrix elements array
+ ++pos;
+ }
}
- }
- // Copy shape for yale construction
- size_t* shape = ALLOC_N(size_t, 2);
- shape[0] = rhs->shape[0];
- shape[1] = rhs->shape[1];
+ // Copy shape for yale construction
+ size_t* shape = ALLOC_N(size_t, 2);
+ shape[0] = rhs->shape[0];
+ shape[1] = rhs->shape[1];
- // Create with minimum possible capacity -- just enough to hold all of the entries
- YALE_STORAGE* lhs = yale_storage_create(l_dtype, shape, 2, shape[0] + ndnz + 1);
- LDType* lhs_a = reinterpret_cast<LDType*>(lhs->a);
- LIType* lhs_ija = reinterpret_cast<LIType*>(lhs->ija);
+ // Create with minimum possible capacity -- just enough to hold all of the entries
+ YALE_STORAGE* lhs = yale_storage_create(l_dtype, shape, 2, shape[0] + ndnz + 1);
+ LDType* lhs_a = reinterpret_cast<LDType*>(lhs->a);
+ LIType* lhs_ija = reinterpret_cast<LIType*>(lhs->ija);
- // Set the zero position in the yale matrix
- lhs_a[ shape[0] ] = R_ZERO;
+ // Set the zero position in the yale matrix
+ lhs_a[ shape[0] ] = R_ZERO;
- // Start just after the zero position.
- LIType ija = lhs->shape[0]+1;
- LIType i;
- pos = 0;
+ // Start just after the zero position.
+ LIType ija = lhs->shape[0]+1;
+ LIType i;
+ pos = 0;
- // Copy contents
- for (i = 0; i < rhs->shape[0]; ++i) {
- // indicate the beginning of a row in the IJA array
- lhs_ija[i] = ija;
+ // Copy contents
+ for (i = 0; i < rhs->shape[0]; ++i) {
+ // indicate the beginning of a row in the IJA array
+ lhs_ija[i] = ija;
- for (LIType j = 0; j < rhs->shape[1]; ++j) {
+ for (LIType j = 0; j < rhs->shape[1]; ++j) {
- if (i == j) { // copy to diagonal
- lhs_a[i] = rhs_elements[pos];
- } else if (rhs_elements[pos] != R_ZERO) { // copy nonzero to LU
- lhs_ija[ija] = j; // write column index
+ if (i == j) { // copy to diagonal
+ lhs_a[i] = rhs_elements[pos];
+ } else if (rhs_elements[pos] != R_ZERO) { // copy nonzero to LU
+ lhs_ija[ija] = j; // write column index
- lhs_a[ija] = rhs_elements[pos];
+ lhs_a[ija] = rhs_elements[pos];
- ++ija;
+ ++ija;
+ }
+ ++pos;
}
- ++pos;
}
+ lhs_ija[i] = ija; // indicate the end of the last row
+
+ lhs->ndnz = ndnz;
+
+ return lhs;
}
- lhs_ija[i] = ija; // indicate the end of the last row
- lhs->ndnz = ndnz;
+ /*
+ * Creation of yale storage from list storage.
+ */
+ template <typename LDType, typename RDType, typename LIType>
+ YALE_STORAGE* create_from_list_storage(const LIST_STORAGE* rhs, dtype_t l_dtype) {
+ NODE *i_curr, *j_curr;
+ size_t ndnz = list_storage_count_nd_elements(rhs);
- return lhs;
-}
+ if (rhs->rank != 2) rb_raise(nm_eStorageTypeError, "can only convert matrices of rank 2 to yale");
-STORAGE* yale_storage_from_dense(const STORAGE* right, dtype_t l_dtype) {
- NAMED_LRI_DTYPE_TEMPLATE_TABLE(ttable, yale_storage_from_dense_template, YALE_STORAGE*, const DENSE_STORAGE* rhs, dtype_t l_dtype);
+ if ((rhs->dtype == RUBYOBJ and (*reinterpret_cast<RubyObject*>(rhs->default_val)) == RubyObject(INT2FIX(0)))
+ || strncmp(reinterpret_cast<const char*>(rhs->default_val), "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DTYPE_SIZES[rhs->dtype]))
+ rb_raise(nm_eStorageTypeError, "list matrix must have default value of 0 to convert to yale");
- itype_t itype = yale_storage_itype((const YALE_STORAGE*)right);
- return (STORAGE*)ttable[l_dtype][right->dtype][itype]((const DENSE_STORAGE*)right, l_dtype);
-}
+ // Copy shape for yale construction
+ size_t* shape = ALLOC_N(size_t, 2);
+ shape[0] = rhs->shape[0];
+ shape[1] = rhs->shape[1];
+ YALE_STORAGE* lhs = yale_storage_create(l_dtype, shape, 2, shape[0] + ndnz + 1);
+ clear_diagonal_and_zero<LIType>(lhs); // clear the diagonal and the zero location.
+ LIType* lhs_ija = reinterpret_cast<LIType*>(lhs->ija);
+ LDType* lhs_a = reinterpret_cast<LDType*>(lhs->a);
-/*
- * Creation of yale storage from list storage.
- */
-template <typename LDType, typename RDType, typename LIType>
-YALE_STORAGE* yale_storage_from_list_template(const LIST_STORAGE* rhs, dtype_t l_dtype) {
- NODE *i_curr, *j_curr;
- size_t ndnz = list_storage_count_nd_elements(rhs);
+ LIType ija = lhs->shape[0]+1;
- if (rhs->rank != 2) rb_raise(nm_eStorageTypeError, "can only convert matrices of rank 2 to yale");
+ for (i_curr = rhs->rows->first; i_curr; i_curr = i_curr->next) {
- if ((rhs->dtype == RUBYOBJ and (*reinterpret_cast<RubyObject*>(rhs->default_val)) == RubyObject(INT2FIX(0)))
- || strncmp(reinterpret_cast<const char*>(rhs->default_val), "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DTYPE_SIZES[rhs->dtype]))
- rb_raise(nm_eStorageTypeError, "list matrix must have default value of 0 to convert to yale");
+ // indicate the beginning of a row in the IJA array
+ lhs_ija[i_curr->key] = ija;
+ for (j_curr = ((LIST*)(i_curr->val))->first; j_curr; j_curr = j_curr->next) {
+ LDType cast_jcurr_val = *reinterpret_cast<RDType*>(j_curr->val);
- // Copy shape for yale construction
- size_t* shape = ALLOC_N(size_t, 2);
- shape[0] = rhs->shape[0];
- shape[1] = rhs->shape[1];
+ if (i_curr->key == j_curr->key)
+ lhs_a[i_curr->key] = cast_jcurr_val; // set diagonal
+ else {
- YALE_STORAGE* lhs = yale_storage_create(l_dtype, shape, 2, shape[0] + ndnz + 1);
- yale_storage_clear_diagonal_and_zero_template<LIType>(lhs); // clear the diagonal and the zero location.
- LIType* lhs_ija = reinterpret_cast<LIType*>(lhs->ija);
- LDType* lhs_a = reinterpret_cast<LDType*>(lhs->a);
+ lhs_ija[ija] = j_curr->key; // set column value
+ lhs_a[ija] = cast_jcurr_val; // set cell value
- LIType ija = lhs->shape[0]+1;
+ ++ija;
+ }
+ }
- for (i_curr = rhs->rows->first; i_curr; i_curr = i_curr->next) {
+ if (!i_curr->next) lhs_ija[i_curr->key] = ija; // indicate the end of the last row
+ }
- // indicate the beginning of a row in the IJA array
- lhs_ija[i_curr->key] = ija;
+ lhs->ndnz = ndnz;
+ return lhs;
+ }
- for (j_curr = ((LIST*)(i_curr->val))->first; j_curr; j_curr = j_curr->next) {
- LDType cast_jcurr_val = *reinterpret_cast<RDType*>(j_curr->val);
+}; // end of namespace yale_storage
- if (i_curr->key == j_curr->key)
- lhs_a[i_curr->key] = cast_jcurr_val; // set diagonal
- else {
+extern "C" {
- lhs_ija[ija] = j_curr->key; // set column value
- lhs_a[ija] = cast_jcurr_val; // set cell value
+ STORAGE* yale_storage_from_dense(const STORAGE* right, dtype_t l_dtype) {
+ NAMED_LRI_DTYPE_TEMPLATE_TABLE(ttable, yale_storage::create_from_dense_storage, YALE_STORAGE*, const DENSE_STORAGE* rhs, dtype_t l_dtype);
- ++ija;
- }
- }
+ itype_t itype = yale_storage_itype((const YALE_STORAGE*)right);
- if (!i_curr->next) lhs_ija[i_curr->key] = ija; // indicate the end of the last row
+ return (STORAGE*)ttable[l_dtype][right->dtype][itype]((const DENSE_STORAGE*)right, l_dtype);
}
- lhs->ndnz = ndnz;
- return lhs;
-}
-STORAGE* yale_storage_from_list(const STORAGE* right, dtype_t l_dtype) {
- NAMED_LRI_DTYPE_TEMPLATE_TABLE(ttable, yale_storage_from_list_template, YALE_STORAGE*, const LIST_STORAGE* rhs, dtype_t l_dtype);
+ STORAGE* yale_storage_from_list(const STORAGE* right, dtype_t l_dtype) {
+ NAMED_LRI_DTYPE_TEMPLATE_TABLE(ttable, yale_storage::create_from_list_storage, YALE_STORAGE*, const LIST_STORAGE* rhs, dtype_t l_dtype);
- itype_t itype = yale_storage_itype((const YALE_STORAGE*)right);
+ itype_t itype = yale_storage_itype((const YALE_STORAGE*)right);
- return (STORAGE*)ttable[l_dtype][right->dtype][itype]((const LIST_STORAGE*)right, l_dtype);
-}
+ return (STORAGE*)ttable[l_dtype][right->dtype][itype]((const LIST_STORAGE*)right, l_dtype);
+ }
+
+} // end of extern "C"
//////////////////////
// Helper Functions //
@@ -53,40 +53,44 @@
* Macros
*/
-#define NUM_STYPES 3
+extern "C" {
-#define NMATRIX_DTYPE_IS_COMPLEX(s) ((s->dtype == COMPLEX64) or (s->dtype == COMPLEX128))
-#define NMATRIX_DTYPE_IS_FLOAT(s) ((s->dtype == FLOAT32) or (s->dtype == FLOAT64))
-#define NMATRIX_DTYPE_IS_INTEGER(s) (s->dtype <= INT64)
-#define NMATRIX_DTYPE_IS_RATIONAL(s) ((s->dtype == RATIONAL32) or (s->dtype == RATIONAL64) or (s->dtype == RATIONAL128))
-#define NMATRIX_DTYPE_IS_RUBYOBJ(s) (s->dtype == RUBYOBJ)
+ #define NUM_STYPES 3
+ #define NMATRIX_DTYPE_IS_COMPLEX(s) ((s->dtype == COMPLEX64) or (s->dtype == COMPLEX128))
+ #define NMATRIX_DTYPE_IS_FLOAT(s) ((s->dtype == FLOAT32) or (s->dtype == FLOAT64))
+ #define NMATRIX_DTYPE_IS_INTEGER(s) (s->dtype <= INT64)
+ #define NMATRIX_DTYPE_IS_RATIONAL(s) ((s->dtype == RATIONAL32) or (s->dtype == RATIONAL64) or (s->dtype == RATIONAL128))
+ #define NMATRIX_DTYPE_IS_RUBYOBJ(s) (s->dtype == RUBYOBJ)
-/*
- * Types
- */
+ /*
+ * Types
+ */
-/*
- * Data
- */
-extern const char* const STYPE_NAMES[NUM_STYPES];
-extern void (* const STYPE_MARK[NUM_STYPES])(void*);
+ /*
+ * Data
+ */
-/*
- * Functions
- */
+ extern const char* const STYPE_NAMES[NUM_STYPES];
+ extern void (* const STYPE_MARK[NUM_STYPES])(void*);
+
+ /*
+ * Functions
+ */
+
+ /////////////////////////
+ // Copying and Casting //
+ /////////////////////////
-/////////////////////////
-// Copying and Casting //
-/////////////////////////
+ STORAGE* dense_storage_from_list(const STORAGE* right, dtype_t l_dtype);
+ STORAGE* dense_storage_from_yale(const STORAGE* right, dtype_t l_dtype);
+ STORAGE* list_storage_from_dense(const STORAGE* right, dtype_t l_dtype);
+ STORAGE* list_storage_from_yale(const STORAGE* right, dtype_t l_dtype);
+ STORAGE* yale_storage_from_list(const STORAGE* right, dtype_t l_dtype);
+ STORAGE* yale_storage_from_dense(const STORAGE* right, dtype_t l_dtype);
-STORAGE* dense_storage_from_list(const STORAGE* right, dtype_t l_dtype);
-STORAGE* dense_storage_from_yale(const STORAGE* right, dtype_t l_dtype);
-STORAGE* list_storage_from_dense(const STORAGE* right, dtype_t l_dtype);
-STORAGE* list_storage_from_yale(const STORAGE* right, dtype_t l_dtype);
-STORAGE* yale_storage_from_list(const STORAGE* right, dtype_t l_dtype);
-STORAGE* yale_storage_from_dense(const STORAGE* right, dtype_t l_dtype);
+} // end of extern "C" block
#endif // STORAGE_H
Oops, something went wrong.

0 comments on commit e25083f

Please sign in to comment.