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

  • Home
  • SEARCH
  • 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 8833611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:18+00:00 2026-06-14T08:49:18+00:00

I was trying to get a variation of the CUDA matrix transpose sample for

  • 0

I was trying to get a variation of the CUDA matrix transpose sample for all kind of sizes. Briefly, I have to take an input array (double *a) and write it on two different parts (you will notice the different offsets) of a bigger matrix (double *tab). I’m storing the data in row-major format so I’m using this macro for indexing:

#define IDX2L(i,j,ld) (((i)*ld))+(j)) // 0 based index +row-major format

This is the simple code I use.

__global__ void cuda_a_Coalesced(double *tab, int tab_rows, int a_rows, double *a)
{
    __shared__  double tile[16*(16+1)]; 
    int col = threadIdx.x + blockIdx.x * blockDim.x;
    int row = threadIdx.y + blockIdx.y * blockDim.y;

    int col_2, row_2;
    int a_cols=tab_rows-a_rows; // tab_rows-a_rows is the number of columns of a
    int tab_cols=2*tab_rows+2;  // 2*tab_rows+2 is the number of columns of tab

    if( (col<a_cols) && (row<a_rows) ) 
    {
        // Load the data into shared mem
        tile[threadIdx.x+threadIdx.y*(16+1)]=a[IDX2L(row,col,a_cols)];

        // Normal copy (+ offsets)
        tab[IDX2L(row,col+tab_rows+a_rows,tab_cols)]= tile[threadIdx.x+threadIdx.y*(16+1)];

        // New idx
        col_2 = blockIdx.y * blockDim.y + threadIdx.x;
        row_2 = blockIdx.x * blockDim.x + threadIdx.y;
    }
    __syncthreads();

    if( (row_2<a_cols) && (col_2<a_rows) )
        // Transpose (+ other offsets)
        tab[IDX2L(row_2+a_rows,col_2+tab_rows,tab_cols)]= -tile[threadIdx.y+threadIdx.x*(16+1)];

}

The launching parameters are the followings:

b1=(int)ceil((float)a_cols/16);
b2=(int)ceil((float)a_rows/16);
dim bck(b1,b2):dim th(16,16);

cuda_a_Coalesced<<<bck,th>>>(tab,tab_rows,a_rows,a);

Normal copy is always well performed regardless of the size. Transpose copy only works for that sizes that are integer multiple of the block size (as in the CUDA sample). When transpose copy fails, some parts of the operations are right and others not, on a way that I can not exactly predict or track. Note as the idea is to change the index in the shared memory so that the transpose can be written in a coalesced form in the output matrix (due to row major-format).

Someone could tell me the reason why the code only works with that kind of sizes?

Is there any trick to solve this situation?

  • 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-14T08:49:19+00:00Added an answer on June 14, 2026 at 8:49 am

    The problem was due to some undefined threads because the value for col_2 and row_2 was being assigned within an if() statement that no all threads were visiting.

    To solve this situation we can give the value for col_2 and row_2 when we declare these variables and delete the homonimous compute that had place within the mentioned if():

    __shared__  double tile[16*(16+1)];
    
    int col = threadIdx.x + blockIdx.x * blockDim.x;
    int row = threadIdx.y + blockIdx.y * blockDim.y;
    
    int col_2 = blockIdx.y * blockDim.y + threadIdx.x;
    int row_2 = blockIdx.x * blockDim.x + threadIdx.y;
    
    int a_cols=tab_rows-a_rows; 
    int tab_cols=2*tab_rows+2;
    

    Thus, the rest of the code looks like this:

    if( (col<a_cols) && (row<a_rows) ) 
    {
        // Load the data into shared mem
        tile[threadIdx.x+threadIdx.y*(16+1)]=a[IDX2L(row,col,a_cols)];
        // Normal copy (+ offsets)
        tab[IDX2L(row,col+tab_rows+a_rows,tab_cols)]= tile[threadIdx.x+threadIdx.y*(16+1)];
    }
    __syncthreads();
    
    if( (row_2<a_cols) && (col_2<a_rows) )
        // Transpose (+ other offsets)
        tab[IDX2L(row_2+a_rows,col_2+tab_rows,tab_cols)]= -tile[threadIdx.y+threadIdx.x*(16+1)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I have been trying to get this effect for a couple of hours now
I am trying to get a count of messages in my MSMQ. I have
I'm trying to get Java's scanner to keep accepting input until the user types
I am trying to use CRTP with a small variation. I have one derived
I'm trying to get WPF validation to work within the MVVM pattern. In my
alt text http://img42.imageshack.us/img42/4161/blinkthru.png I'm trying to get the validation to not show through my
I'm trying to set form validation with cyrillic alphabet but I get We're sorry,
So i am trying get 2 div-containers which both should contain centered text (Both

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.