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,...
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 this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code. Given Sequential Solution: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program...
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 The program will report The...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT