Question

In: Computer Science

We see that this computes the product of two matrices. Add a new kernel code, called...

We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices.

#include <stdio.h>
#include <math.h>
#include <sys/time.h>

#define TILE_WIDTH 2
#define WIDTH 6

// Kernel function execute by the device (GPU)
__global__ void
product (float *d_a, float *d_b, float *d_c, const int n) {
   int col = blockIdx.x * blockDim.x + threadIdx.x ;
   int row = blockIdx.y * blockDim.y + threadIdx.y ;

   float sum = 0;
   if (row < n && col < n) {
      for (int i = 0 ; i<n ; ++i) {
         sum += d_a[row * n + i ] * d_b[i * n + col] ;
      }
      d_c[row * n + col] = sum;
   }
}


// Utility function to print the input matrix
void printMatrix (float m[][WIDTH]) {
   int i, j;
   for (i = 0; i<WIDTH; ++i) {
      for (j = 0; j< WIDTH; ++j) {
         printf ("%d\t", (int)m[i][j]);
      }
      printf ("\n");
   }
}

// Main function execute by the host (CPU)
int main () {
   // host matrices
   float host_a[WIDTH][WIDTH],
         host_b[WIDTH][WIDTH],
         host_c[WIDTH][WIDTH];

   // device arrays
   float *device_a, *device_b, *device_c;

   int i, j;

   // initialize host matrices using random numbers
   time_t t;
   srand ((unsigned) time(&t));

   for (i = 0; i<WIDTH; ++i) {
      for (j = 0; j<WIDTH; j++) {
         host_a[i][j] = (float) (rand() % 50);
         host_b[i][j] = (float) (rand() % 50);
      }
   }

   printf ("Matrix A:\n");
   printMatrix (host_a);
   printf ("\n");

   printf ("Matrix B:\n");
   printMatrix (host_b);
   printf ("\n");

   // allocate device memory for input matrices
   size_t deviceSize = WIDTH * WIDTH * sizeof (float);
   cudaMalloc ((void **) &device_a, deviceSize);
   cudaMalloc ((void **) &device_b, deviceSize);

   // copy host matrices to device
   cudaMemcpy (device_a, host_a, deviceSize, cudaMemcpyHostToDevice );
   cudaMemcpy (device_b, host_b, deviceSize, cudaMemcpyHostToDevice );

   // allocate device memory to store computed result
   cudaMalloc((void **) &device_c, deviceSize) ;

   dim3 dimBlock (WIDTH, WIDTH);
   dim3 dimGrid (WIDTH/TILE_WIDTH, WIDTH/TILE_WIDTH);
   product<<<dimGrid, dimBlock>>> (device_a, device_b, device_c, WIDTH);

   // copy result from device back to host
   cudaMemcpy (host_c, device_c, deviceSize, cudaMemcpyDeviceToHost);

   // output the computed result matrix
   printf ("A x B: \n");
   printMatrix (host_c);

   cudaFree (device_a);
   cudaFree (device_b);
   cudaFree (device_c);
   return 0;
}

Solutions

Expert Solution


// Utility function to sum of the input matrix
void sum (float m[WIDTH][WIDTH], float n[WIDTH][WIDTH] ){
   int i, j;

float sum[width][width];
   for (i = 0; i<WIDTH; ++i) {
      for (j = 0; j< WIDTH; ++j) {
sum[i][j] = a[i][j] + b[i][j]
}
   }
printMatrix (sum[WIDTH][WIDTH]);
}

The explanation goes as the sum of the Matrixx can be calculated by adding their respective positions of both the Matrix. By respective positions I mean the first place of first Matrixx will be added to the first place of second Matrixx for example if a Matrix od size 3, D2, two position will be added to the two, two position of the second Matrix. After adding both the Matrixx the new some will be stored in a new Matrixx named as Sahab and similarly all the elements will be added after getting the new Matrixx named as Sum will be printing the Matrix with the help of the predefined function that is PrintMatrix that has already been defined into the example.


Related Solutions

Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles...
Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles grows with increasing matrix sizes. 10 points pseudocode 10 points description of code computing cycle growth
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
Write an Assembly code to input two integer numbers from keyboard, computes the division of two...
Write an Assembly code to input two integer numbers from keyboard, computes the division of two numbers WITHOUT using division operator and print out the reminder and quotient to the screen. Note: program using “division operator” will earn no credit for this task. You are ALLOWED to use the “print” and “read” macro
suppose a regression model has two explanatory variables (x and z). If we add a new...
suppose a regression model has two explanatory variables (x and z). If we add a new variable to to the model (m), and this new variable is correlated to x and z, how would we use the new variable m to test the impact of variable x on our dependent variable y when z and m remain the same?
Could we turn this java code into a GUI game? Add a orange and a green...
Could we turn this java code into a GUI game? Add a orange and a green trophy object. Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09. Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09. Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes...
Could we turn this java code into a GUI game? Add a orange and a green...
Could we turn this java code into a GUI game? Add a orange and a green trophy object. Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09. Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09. Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT