[Insight-users] Fast large matrix add

Dženan Zukić dzenanz at gmail.com
Tue Oct 25 08:02:17 EDT 2011


Instead of doing it "by hand", you might use an optimized linear algebra
library. ITK includes VNL, but you may also use something else (I use
Eigen<http://eigen.tuxfamily.org/index.php?title=Main_Page>).
It should be faster, because those libraries are optimized to take advantage
of SIMD instructions such as MMX, SSE2, AVX etc, among other performance
optimizations.

But before that, make sure you are compiling your code in release mode. 12
million multiplications + additions should take much closer to 2ms than
20ms.

About transferring data to GPU and back: you are right, it makes no sense to
do it if you only need to execute 2 operations on each element.

HTH

On Mon, Oct 24, 2011 at 17:36, zlf <jxdw_zlf at yahoo.com.cn> wrote:

> Hi
> I have two 4000x3000 matrics. Say matrix A and matrix C. I want to add the
> two matrics. It tooks me 20ms now.
>
> But in my application. I need it finished in 2ms!
>
> Moreover, I cannot put the calculation in GPU because the data translation
> between memory and GPU is time-costuming.
>
> Anyway to accerlate the calculation in CPU?
>
> #include "stdafx.h"
> #include <iostream>
> #include <time.h>
> #include <conio.h>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
>        short* a = new short[4000*3000];
>        short* b = new short[4000*3000];
>        short* c = new short[4000*3000];
>
>        clock_t cstart, cend;
>        double spend;
>        cstart = clock();
>
>        for(int i = 0 ; i < 1000;++i){
>                #pragma omp parallel for
>                for(int x = 0 ; x < 4000*3000;++x){
>                        //for(int y = 0 ; y < 3000;++y){
>                        short value = a[x] * 3000 + b[x];
>                        c[x] = value;
>                        //}
>                }
>        }
>
>        cend = clock();
>        spend = ((double)(cend-cstart)) / (double)CLOCKS_PER_SEC*1000/1000;
>        printf("spend: %f\n(ms)", spend);
>
>        return 0;
> }
>
> Thanks
>
> Jerry
>
> --
> View this message in context:
> http://itk-insight-users.2283740.n2.nabble.com/Fast-large-matrix-add-tp6925448p6925448.html
> Sent from the ITK Insight Users mailing list archive at Nabble.com.
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20111025/19f4416d/attachment.htm>


More information about the Insight-users mailing list