parent
f7824cb11a
commit
8934bf2ca6
@ -0,0 +1,155 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <arm_neon.h>
|
||||
|
||||
typedef struct {
|
||||
int* values;
|
||||
int* rowIndex;
|
||||
int* colIndex;
|
||||
int nonZeroCount;
|
||||
} SparseMatrix;
|
||||
|
||||
void sparseMatmul(SparseMatrix* A, SparseMatrix* B, SparseMatrix* C) {
|
||||
int currentIndex = 0;
|
||||
int i, j;
|
||||
for (i = 0; i < A->nonZeroCount; i++)
|
||||
{
|
||||
int rowA = A->rowIndex[i];
|
||||
int colA = A->colIndex[i];
|
||||
float valueA = A->values[i];
|
||||
for (j = 0; j < A->nonZeroCount; j++)
|
||||
{
|
||||
int rowB = B->rowIndex[j];
|
||||
int colB = B->colIndex[j];
|
||||
float valueB = B->values[j];
|
||||
if (colA == rowB)
|
||||
{
|
||||
float product = valueA * valueB;
|
||||
int found = 0;
|
||||
int k;
|
||||
for (k = 0; k < currentIndex; k++)
|
||||
{
|
||||
if (C->rowIndex[k] == rowA && C->colIndex[k] == colB){
|
||||
C->values[k] += product;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
C->values[currentIndex] = product;
|
||||
C->rowIndex[currentIndex] = rowA;
|
||||
C->colIndex[currentIndex] = colB;
|
||||
currentIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
C->nonZeroCount = currentIndex;
|
||||
}
|
||||
|
||||
void generate(SparseMatrix* matrix, int rows, int cols, int nonZeroCount){
|
||||
matrix->values = (int*)malloc(sizeof(int) * nonZeroCount);
|
||||
matrix->rowIndex = (int*)malloc(sizeof(int) * nonZeroCount);
|
||||
matrix->colIndex = (int*)malloc(sizeof(int) * nonZeroCount);
|
||||
matrix->nonZeroCount = nonZeroCount;
|
||||
int i;
|
||||
for (i = 0; i < nonZeroCount; i++)
|
||||
{
|
||||
matrix->rowIndex[i] = rand() % rows;
|
||||
matrix->colIndex[i] = rand() % cols;
|
||||
matrix->values[i] = rand() %100;
|
||||
}
|
||||
}
|
||||
|
||||
void matmulNEON(float** A, float** B, float** C, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
float32x4_t vecC = vmovq_n_f32(0);
|
||||
for (int k = 0; k < n; k += 4) {
|
||||
float32x4_t vecA = vld1q_f32(&A[i][k]);
|
||||
float32x4_t vecB = vld1q_f32(&B[k][j]);
|
||||
vecC = vmlaq_f32(vecC, vecA, vecB);
|
||||
}
|
||||
C[i][j] = vgetq_lane_f32(vecC, 0) + vgetq_lane_f32(vecC, 1) +
|
||||
vgetq_lane_f32(vecC, 2) + vgetq_lane_f32(vecC, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_matrix(SparseMatrix* matrix) {
|
||||
free(matrix->values);
|
||||
free(matrix->rowIndex);
|
||||
free(matrix->colIndex);
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
|
||||
// int i;
|
||||
int rowsA = 1000;
|
||||
int rowsB = 2000;
|
||||
int colsB = 1000;
|
||||
int nonZeroCountA = 10000;
|
||||
int nonZeroCountB = 10000;
|
||||
|
||||
SparseMatrix A, B;
|
||||
generate(&A, rowsA, rowsB, nonZeroCountA);
|
||||
generate(&B, rowsB, colsB, nonZeroCountB);
|
||||
|
||||
// for (i = 0; i < A.nonZeroCount; ++i) {
|
||||
// printf("A[%d][%d] = %d\n", A.rowIndex[i], A.colIndex[i], A.values[i]);
|
||||
// }
|
||||
|
||||
// for (i = 0; i < B.nonZeroCount; ++i) {
|
||||
// printf("B[%d][%d] = %d\n", B.rowIndex[i], B.colIndex[i], B.values[i]);
|
||||
// }
|
||||
|
||||
int i;
|
||||
float** matrixA = (float**)malloc(rowsA * rowsB * sizeof(float*));
|
||||
float** matrixB = (float**)malloc(rowsB * colsB * sizeof(float*));
|
||||
float** matrixC = (float**)malloc(rowsA * colsB * sizeof(float*));
|
||||
for (i = 0; i < nonZeroCountA; i++)
|
||||
{
|
||||
int row = A.rowIndex[i];
|
||||
int col = A.colIndex[i];
|
||||
matrixA[row][col] = A.values[i];
|
||||
}
|
||||
for (i = 0; i < nonZeroCountB; i++)
|
||||
{
|
||||
int row = B.rowIndex[i];
|
||||
int col = B.colIndex[i];
|
||||
matrixA[row][col] = B.values[i];
|
||||
}
|
||||
|
||||
|
||||
|
||||
SparseMatrix C;
|
||||
C.values = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int));
|
||||
C.rowIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int));
|
||||
C.colIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int));
|
||||
C.nonZeroCount = 0;
|
||||
|
||||
clock_t start_time1 = clock();
|
||||
sparseMatmul(&A, &B, &C);
|
||||
clock_t end_time1 = clock();
|
||||
double time1 = (double)(end_time1 - start_time1) / CLOCKS_PER_SEC;
|
||||
printf("sparseMatrix %f\n", time1);
|
||||
|
||||
clock_t start_time2 = clock();
|
||||
matmulNEON(matrixA, matrixB, matrixC, rowsA * colsB)
|
||||
clock_t end_time2 = clock();
|
||||
double time2 = (double)(end_time2 - start_time2) / CLOCKS_PER_SEC;
|
||||
printf("sparseMatrix %f\n", time2);
|
||||
|
||||
|
||||
// for (i = 0; i < C.nonZeroCount; ++i) {
|
||||
// printf("C[%d][%d] = %d\n", C.rowIndex[i], C.colIndex[i], C.values[i]);
|
||||
// }
|
||||
|
||||
free_matrix(&A);
|
||||
free_matrix(&B);
|
||||
free_matrix(&C);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue