简体   繁体   中英

Multiply 2 sparse matrices using cusp library

Im new to using cusp library for cuda. I'm trying to implement revised simplex algorithm for CUDA. For that I need to multiply 2 sparse matrices to update the base matrix.

So the question is - how can I multiply 2 sparse matrices(doesn't really matter in what format) using cusp library? Also is there a way to know how many nonzero elements is the result matrix going to contain(for memory allocation purposes)?

I tried:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdlib.h>
#include <stdio.h>
#include "cusparse.h"
#include <cusp/version.h>
#include <cusp/multiply.h>
#include <cusp/array2d.h>
#include <cusp/print.h>
#include <cusp/coo_matrix.h>

int main(void)
{
    cusp::coo_matrix<int,double,cusp::device_memory> A(2,2,2);

    A.values[0] = 1;
    A.row_indices[0] = 0;
    A.column_indices[0]= 0;

    A.values[1] = 1;
    A.row_indices[1] = 1;
    A.column_indices[1]= 1;

    cusp::coo_matrix<int, double, cusp::device_memory> B(2,2,4);

    B.values[0] = 1;
    B.row_indices[0] = 0;
    B.column_indices[0]= 0;

    B.values[1] = 2;
    B.row_indices[1] = 0;
    B.column_indices[1]= 1;

    B.values[2] = 3;
    B.row_indices[2] = 1;
    B.column_indices[2]= 0;

    B.values[3] = 4;
    B.row_indices[3] = 1;
    B.column_indices[3]= 1;

    cusp::print(A);
    cusp::print(B);

    cusp::coo_matrix<int,double, cusp::device_memory> C(2,2,4);

    cusp::multiply(A,B,C);

    cusp::print(C);

}

As cusp:multiply() was the only function for multiplication I found.

cusp v.0.4 CUDA v.5.5

how can I multiply 2 sparse matrices(doesn't really matter in what format) using cusp library?

Yes, cusp::multiply is the correct function for this. Your code works correctly for me.

Also is there a way to know how many nonzero elements is the result matrix going to contain(for memory allocation purposes)?

You don't need to allocate it ahead of time. Change your definition of the C matrix to this:

cusp::coo_matrix<int,double, cusp::device_memory> C;

and your code still works correctly.

Thank you for your answers. This code didnt work for me, but I found the problem. For some reason when I had compute capability set to 3.5 and SM version also to 3.5 it wasnt working. I set it to 2.0/2.1 and its working just fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM