Code Monkey home page Code Monkey logo

hpc_assign1's Introduction

Matrix Multiplication with OpenMP

Code Explanation

This program performs the following steps:

  1. Command-Line Arguments: It accepts two command-line arguments - matrix_size and num_threads. These arguments determine the size of the matrices and the number of threads to use for parallel execution.

  2. Memory Allocation: Dynamic memory allocation is used to create three matrices A, B, and C. These matrices will hold the input matrices and the result of matrix multiplication.

  3. Initialization: Matrices A and B are initialized with the value 2, while matrix C is initialized with zeros.

  4. Parallel Matrix Multiplication: The core matrix multiplication operation is parallelized using OpenMP. The #pragma omp parallel for directive splits the work among multiple threads. Each thread calculates a portion of the result matrix C.

  5. Timing: The program measures the elapsed time for the matrix multiplication using the gettimeofday function. This provides a measure of the program's performance.

  6. Memory Deallocation: After the matrix multiplication is complete, memory is deallocated to prevent memory leaks.

How to Compile and Run

To compile the program, open a terminal and navigate to the directory containing the code. Use the following command:

gcc -o matrix_multiplication matrix_multiplication.c -fopenmp

This command compiles the code with OpenMP support and generates an executable named matrix_multiplication.

To run the program, use the following command:

./matrix_multiplication <matrix_size> <num_threads>

For finding nth power of the matrix:

./matrix_multiplication <matrix_size> <exponent> <num_threads>

Replace <matrix_size> with the desired size of the matrices and <num_threads> with the number of threads you want to use for parallel execution.

./matrix_multiplication 512 2

For nth power: Replace <matrix_size> with the desired size of the matrices and <num_threads> with the number of threads you want to use for parallel execution.

./matrix_multiplication 512 2 4

This will perform matrix multiplication with matrices of size 1000x1000 using 4 threads.

Output

The program will display the size of the matrices, the number of threads used, and the elapsed time for matrix multiplication.

Graph for the same are as below:

  1. OMM ( Threads vs Elapsed Time ) :

    image

    image

    image

  2. Nth Power of Matrix with OMM ( Threads vs Elapsed Time ) :

    image

    image

    image

Conclusion

This C program demonstrates how to use OpenMP to parallelize matrix multiplication, improving the performance of the computation. You can adjust the matrix size and the number of threads to observe the impact on execution time.

Block Matrix Multiplication

# Block Matrix Multiplication

Compile the program using a C compiler. For example:

gcc -o block_matrix_mult block_matrix_mult.c -fopenmp

Here, block_matrix_mult is the name of the executable.

To run the program, use the following command-line format:

./block_matrix_mult <matrix_size> <num_threads> <block_size>

For nth power of a matrix using BMM:

./block_matrix_mult <matrix_size> <exponent> <num_threads> <block_size>

<matrix_size>: The size of the square matrices (e.g., 100 for a 100x100 matrix). <block_size>: The size of the square blocks (e.g., 10 for a 10x10 block). For example, to multiply two 100x100 matrices using 10x10 blocks:

./block_matrix_mult 1024 6 4

For nth power:

./block_matrix_mult 1024 2 6 4

Output

The program will display the size of the matrices, the block size, and the result of the multiplication.

  1. BMM ( Threads vs Elapsed Time ) :

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

    image

  2. Nth Power of Matrix with BMM ( Threads vs Elapsed Time ) :

    image

    image

Memory Management

The program dynamically allocates memory for matrices A, B, and C, as well as for the block buffers. It frees the allocated memory after the multiplication is complete to prevent memory leaks.

hpc_assign1's People

Contributors

batul02 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.