Question

In: Computer Science

Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...

Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together.

The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line.

Finally, the result must be stored in a text file.

Solutions

Expert Solution

The following program can be used to implement the given problem in C++.

// CPP Program to multiply two matrix using pthreads 
#include <bits/stdc++.h> 
using namespace std; 
int step_i = 0; 
  
void* multi(void* arg) 
{ 
    int core = step_i++; 
  
    // Each thread computes 1/4th of matrix multiplication 
    for (int i = core * ma1 / 4; i < (core + 1) * ma1 / 4; i++)  
        for (int j = 0; j < mb1; j++)  
            for (int k = 0; k < mb2; k++)  
                matC[i][j] += matA[i][k] * matB[k][j]; 
} 
  
// Driver Code 
int main() 
{
    fstream file;
    string word, filename;
    
    filename = "example.txt";
    file.open(filename.c_str());
    
    cout<<"Enter dimensions of the first matrix :";
    int ma1, ma2, mb1, mb2;
    cin>>ma1>>ma2;
    
    cout<<"Enter dimensions of the second matrix :";
    cin>>mb1>>mb2;
    
    int matA[ma1][ma2];
    int matB[mb1][mb2];
    
    
    if(ma2 != mb1)
    {
        cout<<"The matrices can't be multiplied.";
        return 0;
    }
    
    int matC[ma1][mb2];
    int MAX_THREAD = ma2;
    
    for(int i = 0; i<ma1; i++)
    {
        for(int j = 0; j<ma2; j++)
        {
            file>>word;
            matA[i][j] = word;
        }
    }
    
    for(int i = 0; i<mb1; i++)
    {
        for(int j = 0; j<mb2; j++)
        {
            file>>word;
            matB[i][j] = word;
        }
    }
  
    // Displaying matA 
    cout << endl 
         << "Matrix A" << endl; 
    for (int i = 0; i < ma1; i++) { 
        for (int j = 0; j < ma2; j++)  
            cout << matA[i][j] << " "; 
        cout << endl; 
    } 
  
    // Displaying matB 
    cout << endl 
         << "Matrix B" << endl; 
    for (int i = 0; i < mb1; i++) { 
        for (int j = 0; j < mb2; j++)  
            cout << matB[i][j] << " ";         
        cout << endl; 
    } 
  
    // declaring four threads 
    pthread_t threads[MAX_THREAD]; 
  
    // Creating four threads, each evaluating its own part 
    for (int i = 0; i < MAX_THREAD; i++) { 
        int* p; 
        pthread_create(&threads[i], NULL, multi, (void*)(p)); 
    } 
  
    // joining and waiting for all threads to complete 
    for (int i = 0; i < MAX_THREAD; i++)  
        pthread_join(threads[i], NULL);     
  
    // Displaying the result matrix 
    cout << endl 
         << "Multiplication of A and B" << endl; 
    for (int i = 0; i < ma1; i++) { 
        for (int j = 0; j < mb2; j++)  
            cout << matC[i][j] << " ";         
        cout << endl; 
    } 
    return 0; 
} 

Related Solutions

Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
1. Using the pThread library, write a multithreaded program that calculates various statistical values for a...
1. Using the pThread library, write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Write a program using c, c++, or java that have four dynamic memory partitions of size...
Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
You must write a C program that prompts the user for numbers and multiplies them using...
You must write a C program that prompts the user for numbers and multiplies them using "a la russe" multiplication. No input is allowed at execution time (no command line input). The program MUST DISPLAY YOUR BANNER LOGO as part of a prompt to the user. A menu must allow the user to put in two numbers at the same time or each of two numbers one at a time. The valid range of values for each number is 0...
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays....
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays. splice() must create the third array and return a pointer to main(). Main() must capture the pointer returned from the splice() function. Sol'n so far, I get the results of the first and second array, but it isn't showing the results of the spliced function: #include <iostream> using namespace std; // Function int* splice() inserts the 2nd array into 1st array starting at int...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !! * DO not use [ ] 'brackets', focus on using malloc, calloc, etc... Program will ask user for the name of the text file to be read Read the datafile with format: 1 2 1 1 2 3 4 So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT