An Efficient Way to Subtract Two Matrix With Different Data Type

4 views
Skip to first unread message

aran nokan

unread,
Sep 9, 2021, 1:45:04 PM9/9/21
to MAGMA User
Hi,

I have defined two matrix like this:

double *P_DA;
magma_dmalloc_pinned( &P_DA, M*(M/4) );

float *P_SA;
magma_smalloc_pinned( &P_SA, M*(M/4) );


 #define P_DA(i_, j_) (P_DA  + (i_)       + (j_)*M/4)
 #define  P_SA(i_, j_) (P_SA  + (i_)       + (j_)*M/4)

And now I want to get the difference between them as a variable (something like a norm definition).

So I tried to use 2 loops to get that. But here the problem is the difference of data type and I am not able to cast each element by something like (float).

sub_ft+=abs(P_DA(i, j) - P_SA(i, j));

So what is the efficient way for doing this?

Best regards,
A.N.

Stanimire Tomov

unread,
Sep 9, 2021, 4:14:31 PM9/9/21
to aran nokan, MAGMA User
Hi A.N.,

Since these matrices are on the CPU, you can use routines like LAPACK’s dlag2s or slag2d
to make th metrics that you want to subtract to be the same precision and then subtract and
compute norm also through LAPACK.
MAGMA has similar routines if the matrices are on the GPU memory (magmablas_dlag2s and magmablas_slag2d).

If you do it in your code, like in the example below, if sub_fl is double, all you need to do is

sub_ft+=fabs(P_DA(i, j) - (double)P_SA(i, j));

or if sub_fl is float, do 

sub_ft+=fabsf((float)P_DA(i, j) - (double)P_SA(i, j));

Note that I also changed abs (in math.h this is for integers) to fabs for double and fobs for float data.

Best regards,
Stan

--
You received this message because you are subscribed to the Google Groups "MAGMA User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to magma-user+...@icl.utk.edu.
To view this discussion on the web visit https://groups.google.com/a/icl.utk.edu/d/msgid/magma-user/CAKHt_YaVJUNcUx4CQitEDRaP53MdQRRy9QHjcDd0ej%2BgLBPKAQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages