Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:46:48+00:00 2026-06-11T16:46:48+00:00

I am using mex bridge to perform some operations on Sparse matrices from Matlab.

  • 0

I am using mex bridge to perform some operations on Sparse matrices from Matlab.
For that I need to convert input matrix into CSR (compressed row storage) format, since Matlab stores the sparse matrices in CSC (compressed column storage).

I was able to get value array and column_indices array. However, I am struggling to get row_pointer array for CSR format.Is there any C library that can help in conversion from CSC to CSR ?

Further, while writing a CUDA kernel, will it be efficient to use CSR format for sparse operations or should I just use following arrays :- row indices, column indices and values?

Which on would give me more control over the data, minimizing the number for-loops in the custom kernel?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:46:49+00:00Added an answer on June 11, 2026 at 4:46 pm

    I ended up converting CSC format from Matlab to CSR using CUSP library as follows.

    After getting the matrix A from matlab and I got its row,col and values vectors and I copied them in respective thrust::host_vector created for each of them.

    After that I created two cusp::array1d of type Indices and Values as follows.

        typedef typename cusp::array1d<int,cusp::host_memory>Indices;   
        typedef typename cusp::array1d<float,cusp::host_memory>Values;
        Indices row_indices(rows.begin(),rows.end());
        Indices col_indices(cols.begin(),cols.end());
        Values  Vals(Val.begin(),Val.end());
    

    where rows, cols and Val are thrust::host_vector that I got from Matlab.

    After that I created a cusp::coo_matrix_view as given below.

    typedef cusp::coo_matrix_view<Indices,Indices,Values>HostView;
    HostView Ah(m,n,NNZa,row_indices,col_indices,Vals);
    

    where m,n and NNZa are the parameters that I get from mex functions of sparse matrices.

    I copied this view matrix to cusp::csr_matrixin device memory with proper dimensions set as given below.

        cusp::csr_matrix<int,float,cusp::device_memory>CSR(m,n,NNZa);
        CSR = Ah;   
    

    After that I just copied the three individual content arrays of this CSR matrix back to the host using thrust::raw_pointer_cast where arrays with proper dimension are already mxCalloced as given below.

     cudaMemcpy(Acol,thrust::raw_pointer_cast(&CSR.column_indices[0]),sizeof(int)*(NNZa),cudaMemcpyDeviceToHost);
     cudaMemcpy(Aptr,thrust::raw_pointer_cast(&CSR.row_offsets[0]),sizeof(int)*(n+1),cudaMemcpyDeviceToHost);
     cudaMemcpy(Aval,thrust::raw_pointer_cast(&CSR.values[0]),sizeof(float)*(NNZa),cudaMemcpyDeviceToHost);
    

    Hope this is useful to anyone who is using CUSP with Matlab

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am linking some fortran code (f90) from matlab using mex and I am
I am getting started with using MEX files for MATLAB (R2012a) compiled from C
I have created a sparse matrix using MEX and also created a sparse matrix
I have a Mex-function (a function in c++ that you can call from Matlab)
I am trying to write C++ programs and integrate it with MATLAB using mex
I'm using MATLAB's deployment tool to compile a simple project which uses a mex
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in
How to create a 2d sparse matrix in a MEX-file written in C. After
Normally in MATLAB I can compile a mex file which uses OpenCV functions using:
I am writing a mex-file (using C++) that will accept a memory address as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.