Table of Contents
Example
Output
Home Backend Development C++ Use pthread to implement matrix addition and subtraction in C/C++

Use pthread to implement matrix addition and subtraction in C/C++

Aug 28, 2023 am 09:05 AM
matrix pthread addition Subtraction

Use pthread to implement matrix addition and subtraction in C/C++

Here we will see how to perform matrix addition and subtraction using a multi-threaded environment. pthread is used to execute multiple threads simultaneously in C or C++.

There are two matrices A and B. The order of each matrix is ​​(m x n). Each thread will get each row and perform addition or subtraction. Therefore, for m rows, there are m different threads.

Example

#include<iostream>
#include <pthread.h>
#include <cstdlib>
#include <cstdint>
#define CORE 3
#define MAX 3
using namespace std;
int AMat[MAX][MAX] = {{10, 20, 30},
   {40, 50, 60},
   {70, 80, 50}
};
int BMat[MAX][MAX] = {{80, 60, 20},
   {30, 20, 15},
   {10, 14, 35}
};
pthread_t thread[CORE * 2];
int add[MAX][MAX], sub[MAX][MAX];
void* addMatrices(void* arg) {
   intptr_t core = (intptr_t)arg;
   // Each thread computes 1/3rd of matrix addition
   for (int i = core * MAX / 3; i < (core + 1) * MAX / 3; i++) {
      for (int j = 0; j < MAX; j++) {
         add[i][j] = AMat[i][j] + BMat[i][j];
      }
   }
}
void* subtraction(void* arg) {
   intptr_t core = (intptr_t)arg;
   // Each thread computes 1/3rd of matrix subtraction
   for (int i = core * MAX / 3; i < (core + 1) * MAX / 3; i++) {
      for (int j = 0; j < MAX; j++) {
         sub[i][j] = AMat[i][j] - BMat[i][j];
      }
   }
}
void display(){
   cout << "Matrix A: " << endl;
   for(int i = 0; i < MAX; i++) {
      for(int j = 0; j < MAX; j++) {
         cout << AMat[i][j] << " ";
      }
      cout << endl;
   }
   cout << "\nMatrix B: " << endl;
   for(int i = 0; i < MAX; i++) {
      for(int j = 0; j < MAX; j++) {
         cout << BMat[i][j] << " ";
      }
      cout << endl;
   }
}
void displayRes(){
   cout << "\nAddition: " << endl;
   for(int i = 0; i < MAX; i++) {
      for(int j = 0; j < MAX; j++) {
         cout << add[i][j] << " ";
      }
      cout << endl;
   }
   cout << "\nSubtraction: " << endl;
   for(int i = 0; i < MAX; i++) {
      for(int j = 0; j < MAX; j++) {
         cout << sub[i][j] << " ";
      }
      cout << endl;
   }
}
main() {
   display();
   int step = 0;
   for (int i = 0; i < CORE; i++) {
      pthread_create(&thread[i], NULL, &addMatrices, (void*)step);
      pthread_create(&thread[i + CORE], NULL, &subtraction, (void*)step);
      step++;
   }
   for (int i = 0; i < CORE * 2; i++) {
      pthread_join(thread[i], NULL);
   }
   displayRes();
}
Copy after login

Output

Matrix A:
10 20 30
40 50 60
70 80 50
Matrix B:
80 60 20
30 20 15
10 14 35
Addition:
90 80 50
70 70 75
80 94 85
Subtraction:
-70 -40 10
10 30 45
60 66 15
Copy after login

The above is the detailed content of Use pthread to implement matrix addition and subtraction in C/C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Nov 20, 2023 pm 05:25 PM

In the first article of this series, we discussed the connections and differences between artificial intelligence, machine learning, deep learning, data science, and more. We also made some hard choices about the programming languages, tools, and more that the entire series would use. Finally, we also introduced a little bit of matrix knowledge. In this article, we will discuss in depth the matrix, the core of artificial intelligence. But before that, let’s first understand the history of artificial intelligence. Why do we need to understand the history of artificial intelligence? There have been many AI booms in history, but in many cases the huge expectations for AI's potential failed to materialize. Understanding the history of artificial intelligence can help us see whether this wave of artificial intelligence will create miracles or is just another bubble about to burst. us

How to calculate the determinant of a matrix or ndArray using numpy in Python? How to calculate the determinant of a matrix or ndArray using numpy in Python? Aug 18, 2023 pm 11:57 PM

In this article, we will learn how to calculate the determinant of a matrix using the numpy library in Python. The determinant of a matrix is ​​a scalar value that can represent the matrix in compact form. It is a useful quantity in linear algebra and has numerous applications in various fields including physics, engineering, and computer science. In this article, we will first discuss the definition and properties of determinants. We will then learn how to use numpy to calculate the determinant of a matrix and see how it is used in practice through some examples. Thedeterminantofamatrixisascalarvaluethatcanbeusedtodescribethepropertie

Python program to calculate the sum of the right diagonal elements of a matrix Python program to calculate the sum of the right diagonal elements of a matrix Aug 19, 2023 am 11:29 AM

A popular general-purpose programming language is Python. It is used in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python has a simple and easy-to-understand syntax that is suitable for beginners. In this article, we will use Python to calculate the sum of the right diagonal of a matrix. What is a matrix? In mathematics, we use a rectangular array or matrix to describe a mathematical object or its properties. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows and columns. . For example -234512367574 Therefore, this is a matrix with 3 rows and 4 columns, expressed as a 3*4 matrix. Now, there are two diagonals in the matrix, the primary diagonal and the secondary diagonal

How to write Python addition code_Python addition code writing tutorial How to write Python addition code_Python addition code writing tutorial Apr 02, 2024 pm 06:16 PM

1. First open the online python editing page. 2. Then enter the program code in the program area. 3. Then click Run in the upper left corner, enter the first addend, press Enter, and enter the second addend. 4. Finally press Enter to get the sum of the two addends. This is simple python addition code.

Divide two integers without using the multiplication, division, and modulo operators Divide two integers without using the multiplication, division, and modulo operators Sep 21, 2023 pm 12:41 PM

In this problem, we only need to divide two integers without using multiplication, division and modulo operators. Although we can use addition, multiplication or bit operations. The problem statement states that we will get two integers x and y. Without using multiplication, division, or the modulo operator, we need to determine the quotient of x divided by y. Example Input: x=15, y=5 Output: 3 Input: x=10, y=4 Output: 2 Input: x=-20, y=3 Output: -6 Method Method 1 (use simple math) here In this method, we will use a simple mathematical algorithm. Here is a step-by-step explanation of the steps we will follow - we will keep subtracting the divisor (i.e. y) from the dividend (i.e. x) until x is greater than or equal to y. when y is greater than x

Python program to multiply two matrices using multidimensional arrays Python program to multiply two matrices using multidimensional arrays Sep 11, 2023 pm 05:09 PM

A matrix is ​​a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an mXn matrix, and m and n are called its dimensions. A matrix is ​​a two-dimensional array created in Python using lists or NumPy arrays. In general, matrix multiplication can be done by multiplying the rows of the first matrix by the columns of the second matrix. Here, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Input and output scenario Suppose we have two matrices A and B. The dimensions of these two matrices are 2X3 and 3X2 respectively. The resulting matrix after multiplication will have 2 rows and 1 column. [b1,b2][a1,a2,a3]*[b3,b4]=[a1*b1+a2*b2+a3*a3][a4,a5,a6][b5,b6][a4*b2+a

C program to compare two matrices for equality C program to compare two matrices for equality Aug 31, 2023 pm 01:13 PM

The user must enter the order of the two matrices as well as the elements of both matrices. Then, compare the two matrices. Two matrices are equal if both matrix elements and sizes are equal. If the matrices are equal in size but not equal in elements, then the matrices are shown to be comparable but not equal. If the sizes and elements do not match, the display matrices cannot be compared. The following program is a C program, used to compare whether two matrices are equal-#include<stdio.h>#include<conio.h>main(){ intA[10][10],B[10][10]; in

Oracle database operation skills: detailed explanation of subtraction operation Oracle database operation skills: detailed explanation of subtraction operation Mar 02, 2024 pm 06:15 PM

As a powerful relational database management system, Oracle database provides a wealth of computing operations to meet user needs. In daily database operations, subtraction operation is a common and important operation. It can help us realize the subtraction operation of data to obtain the results we need. This article will discuss in detail the techniques related to subtraction operations in Oracle database, and give specific code examples to help readers better understand and use this function. 1. Basic concepts of subtraction operations in Oracle data

See all articles