Skip to content

Commit

Permalink
add a new method to add a FLOATVAL scalar to the matrix. Use this to …
Browse files Browse the repository at this point in the history
…add a better MULTI for DEFAULT add. Also use the function I added last commit to normalize the lazy transpose state to make the MULTI for NumMatrix2D add much cleaner
  • Loading branch information
Whiteknight committed Jun 21, 2010
1 parent 56d826f commit f733a83
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 76 deletions.
110 changes: 45 additions & 65 deletions src/pmc/nummatrix2d.pmc
Expand Up @@ -99,6 +99,23 @@ init_from_pmc_array(PARROT_INTERP, PMC * self, INTVAL rows_size,
}
}

static void
add_scalar_float(PARROT_INTERP, PMC * self, FLOATVAL v)
{
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
FLOATVAL * const s = attrs->storage;
INTVAL i, j;

/* TODO: See if BLAS has a routine to do this */
for (j = 0; j < cols_size; j++) {
for (i = 0; i < rows_size; i++) {
ITEM_XY_ROWMAJOR(s, rows_size, cols_size, i, j) += v;
}
}
}

pmclass NumMatrix2D dynpmc auto_attrs provides matrix {
ATTR FLOATVAL * storage;
ATTR INTVAL rows;
Expand Down Expand Up @@ -290,87 +307,50 @@ pmclass NumMatrix2D dynpmc auto_attrs provides matrix {
return pstr;
}

/*

=item* add

=cut

*/

MULTI PMC *add(NumMatrix2D *value, PMC *dest) {
int i = 0, j = 0;
INTVAL rows_size, cols_size;
Parrot_NumMatrix2D_attributes * const selfattr = PARROT_NUMMATRIX2D(SELF);
Parrot_NumMatrix2D_attributes * const valattr = PARROT_NUMMATRIX2D(value);
Parrot_NumMatrix2D_attributes * destattr;
const INTVAL rows_size = selfattr->rows;
const INTVAL cols_size = selfattr->cols;
const INTVAL storage_size = rows_size * cols_size;

rows_size = selfattr->rows;
cols_size = selfattr->cols;

if (rows_size != valattr->rows || cols_size != valattr->cols) {
/* XXX: Throw a better exception. */
if (rows_size != valattr->rows || cols_size != valattr->cols)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"NumMatrix2D: Matrix dimensions must match in add.");
}

if ((IS_TRANSPOSED(selfattr->flags) && ! IS_TRANSPOSED(valattr->flags))
|| (IS_TRANSPOSED(valattr->flags) && ! IS_TRANSPOSED(selfattr->flags))) {
FLOATVAL *sstor = selfattr->storage,
*vstor = valattr->storage;
FLOATVAL *dstor = NULL;

dest = Parrot_pmc_new(interp, VTABLE_type(interp, pmc));
resize_matrix(interp, dest, rows_size - 1, cols_size - 1);
destattr = (Parrot_NumMatrix2D_attributes *) PARROT_NUMMATRIX2D(dest);
dstor = destattr->storage;

if (IS_TRANSPOSED(selfattr->flags)) {
for (i = 0; i < rows_size; ++i) {
for (j = 0; j < cols_size; ++j) {
ITEM_XY_ROWMAJOR(dstor, rows_size, cols_size, i, j) =
ITEM_XY_COLMAJOR(sstor, rows_size, cols_size, i, j)
+ ITEM_XY_ROWMAJOR(vstor, rows_size, cols_size, i, j);
}
}
}
else {
for (i = 0; i < rows_size; ++i) {
for (j = 0; j < cols_size; ++j) {
ITEM_XY_ROWMAJOR(dstor, rows_size, cols_size, i, j) =
ITEM_XY_ROWMAJOR(sstor, rows_size, cols_size, i, j)
+ ITEM_XY_COLMAJOR(vstor, rows_size, cols_size, i, j);
}
}
}
}
else {
dest = VTABLE_clone(INTERP, value);
destattr = (Parrot_NumMatrix2D_attributes *) PARROT_NUMMATRIX2D(dest);

cblas_daxpy(rows_size*cols_size, 1, selfattr->storage, 1, destattr->storage, 1);
}

normalize_lazy_transpose(INTERP, SELF);
normalize_lazy_transpose(INTERP, value);
dest = VTABLE_clone(INTERP, value);
destattr = PARROT_NUMMATRIX2D(dest);
cblas_daxpy(storage_size, 1, selfattr->storage, 1, destattr->storage, 1);
return dest;
}

MULTI PMC *add(Float *value, PMC *dest) {
MULTI PMC *add(DEFAULT *value, PMC *dest) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(SELF);
Parrot_NumMatrix2D_attributes * dest_attrs;
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
FLOATVAL * const s = attrs->storage;
FLOATVAL * dest_s;
INTVAL i, j;

dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
resize_matrix(INTERP, dest, rows_size - 1, cols_size - 1);
dest_attrs = PARROT_NUMMATRIX2D(dest);
dest_s = dest_attrs->storage;

/* TODO: See if BLAS has a routine to do this */
for (j = 0; j < cols_size; j++) {
for (i = 0; i < rows_size; i++) {
ITEM_XY_ROWMAJOR(dest_s, rows_size, cols_size, i, j) =
ITEM_XY_ROWMAJOR(s, rows_size, cols_size, i, j) + v;
}
}
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, v);
return dest;
}

/*

=item* multiply

=cut

*/

MULTI PMC *multiply(NumMatrix2D *value, PMC *dest) {
INTVAL rows_size = 0, cols_size = 0, sflags = 0, vflags = 0;

Expand Down
25 changes: 14 additions & 11 deletions t/run_test
@@ -1,27 +1,30 @@
#! parrot-nqp
our @ARGS;
main(@ARGS);

INIT {
pir::load_bytecode('./library/kakapo_full.pbc');
Nqp::compile_file('t/testlib/matrixtest.nqp');
pir::loadlib__ps("./linalg_group");
}

class MyProgram is Program {
#class MyProgram is Program {
method main(*@args) {
for @args {
my $test := $_;
#for @args {
my $test := "nummatrix2d.t";
my $sub := Nqp::compile_file("t/pmc/" ~ $test);
$sub[0]();
}
#}
}
}
#}

INIT {
Program::instance(
MyProgram.new( :from_parrot )
);
}
#INIT {
#Program::instance(
# MyProgram.new( :from_parrot )
#);
#}

#Program::instance().run;

Program::instance().run;


0 comments on commit f733a83

Please sign in to comment.