提交记录 16932
| 提交时间 |
评测时间 |
| 2021-11-03 19:49:05 |
2021-11-03 19:49:07 |
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <chrono>
#include <thread>
#include <nmmintrin.h>
#include <immintrin.h>
namespace Matrix {
class matrix {
public:
static const int n = 740;
static const int m = 740;
float *matrix;
void createRandomMatrix() {
matrix = new float[m * n];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
matrix[n * r + c] = rand() % 10;
}
}
}
void print() {
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
std::cout << matrix[n * r + c] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
void createEmptyMatrix() {
matrix = new float[m * n];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
matrix[n * r + c] = 0;
}
}
}
void deleteMatrix() {
delete[] matrix;
}
};
}
using namespace std::chrono;
void matrixmult(Matrix::matrix *Cptr, Matrix::matrix *Aptr, Matrix::matrix *Bptr, int upperbound,
int lowerbound = 0) {
for (int i = lowerbound; i < upperbound; i++) {
for (int k = 0; k < Bptr->n; k += 4) {
float zero = 0;
__m128 sum = _mm_broadcast_ss(&zero);
for (int j = 0; j < Aptr->n; j++) {
__m128 a = _mm_broadcast_ss(Aptr->matrix + Aptr->n * i + j);
__m128 *b = (__m128 *)(Bptr->matrix + (Bptr->n * j + k));
__m128 product = _mm_mul_ps(a, *b);
sum = _mm_add_ps(sum, product);
//std::cout << "running..."<<i;
}
_mm_store_ps(Cptr->matrix + Cptr->n * i + k, sum);
}
}
return;
}
int main() {
std::cout << "Matrix multiplication\n";
/*time_t startWOT, stopWOT, startWT, stopWT;*/
Matrix::matrix matrixA;
Matrix::matrix matrixB;
Matrix::matrix matrixCWT;
Matrix::matrix matrixCWOT;
Matrix::matrix *matrixCWOTPtr = &matrixCWOT; //Pointer verweist auf den Speicher einer anderen Variable
Matrix::matrix *matrixAPtr = &matrixA;
Matrix::matrix *matrixBPtr = &matrixB;
Matrix::matrix *matrixCWTPtr = &matrixCWT;
matrixA.createRandomMatrix();
matrixB.createRandomMatrix();
matrixCWT.createEmptyMatrix();
matrixCWOT.createEmptyMatrix();
std::cout << "\n[+] Single Core calculation started. \n[*] Calculating...";
auto startWOT = high_resolution_clock::now();
matrixmult(matrixCWOTPtr, matrixAPtr, matrixBPtr, matrixAPtr->m);
auto stopWOT = high_resolution_clock::now();
//double durationWOT = double(stopWOT - startWOT);
std::cout << "\n[+] Single Core calculation finished \n[+] Duration: " << duration<double>
(stopWOT - startWOT).count() << " seconds";
std::cout << "\n[+] Multithreaded calculation started. \n[*] Calculating...";
auto startWT = high_resolution_clock::now();
//threadedmult(matrixCWTPtr, matrixAPtr, matrixBPtr);
auto stopWT = high_resolution_clock::now();
std::cout << "\n[+] Multithreaded calculation finished \n[+] Duration: " << duration<double>
(stopWT - startWT).count() << " seconds";
//matrixA.print();
//matrixB.print();
//matrixCWT.print();
matrixA.deleteMatrix();
matrixB.deleteMatrix();
matrixCWT.deleteMatrix();
}
| Compilation | N/A | N/A | Compile Error | Score: N/A | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2026-03-18 10:08:22 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠