Compare commits

..

No commits in common. 'main' and 'main' have entirely different histories.
main ... main

@ -1,48 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10000000
void vectorAdd(float* A, float* B, float* C, int size) {
int i;
for (i = 0; i < size; ++i) {
C[i] = A[i] + B[i];
}
}
int main() {
float* A = (float*)malloc(SIZE * sizeof(float));
float* B = (float*)malloc(SIZE * sizeof(float));
float* C = (float*)malloc(SIZE * sizeof(float));
if (A == NULL || B == NULL || C == NULL) {
return 1;
}
// float A[SIZE], B[SIZE], C[SIZE];
srand((unsigned)time(NULL));
int i;
for (i = 0; i < SIZE; ++i) {
A[i] = rand() % 100;
B[i] = rand() % 100;
}
clock_t start_time = clock();
vectorAdd(A, B, C, SIZE);
clock_t end_time = clock();
double time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("%f\n", time);
// for (i = 0; i < SIZE; ++i) {
// printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]);
// }
free(A);
free(B);
free(C);
return 0;
}

@ -1,59 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <arm_neon.h>
#define SIZE 10000000
void vectorAdd(float* A, float* B, float* C, int size) {
int i;
for (i = 0; i < size; ++i) {
C[i] = A[i] + B[i];
}
}
void vectorAddNEON(float* A, float* B, float* C, int size) {
int i;
for (i = 0; i <= size - 4; i += 4) {
float32x4_t vecA = vld1q_f32(&A[i]);
float32x4_t vecB = vld1q_f32(&B[i]);
float32x4_t vecC = vaddq_f32(vecA, vecB);
vst1q_f32(&C[i], vecC);
}
for (; i < size; ++i) {
C[i] = A[i] + B[i];
}
}
int main() {
float* A = (float*)malloc(SIZE * sizeof(float));
float* B = (float*)malloc(SIZE * sizeof(float));
float* C = (float*)malloc(SIZE * sizeof(float));
if (A == NULL || B == NULL || C == NULL) {
return 1;
}
srand((unsigned)time(NULL));
for (int i = 0; i < SIZE; ++i) {
A[i] = (float)(rand() % 100);
B[i] = (float)(rand() % 100);
}
clock_t start_time = clock();
vectorAdd(A, B, C, SIZE);
clock_t end_time = clock();
double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("tradition: %f", time1);
start_time = clock();
vectorAddNEON(A, B, C, SIZE);
end_time = clock();
time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("NEON %f",time1);
free(A);
free(B);
free(C);
}

@ -1,52 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024
void matmul(float** A, float** B, float** C, int n) {
int i, j, k;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
C[i][j] = 0;
for (k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main() {
float** A = (float**)malloc(SIZE * sizeof(float*));
float** B = (float**)malloc(SIZE * sizeof(float*));
float** C = (float**)malloc(SIZE * sizeof(float*));
int i, j;
for (i = 0; i < SIZE; ++i) {
A[i] = (float*)malloc(SIZE * sizeof(float));
B[i] = (float*)malloc(SIZE * sizeof(float));
C[i] = (float*)malloc(SIZE * sizeof(float));
}
srand(time(0));
for (i = 0; i < SIZE; ++i) {
for (j = 0; j < SIZE; ++j) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
}
clock_t start = clock();
matmul(A, B, C, SIZE);
clock_t end = clock();
double time = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f\n", time);
for (i = 0; i < SIZE; ++i) {
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A);
free(B);
free(C);
return 0;
}

@ -1,75 +0,0 @@
#include <arm_neon.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024
void matmul(float** A, float** B, float** C, int n) {
int i, j, k;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
C[i][j] = 0;
for (k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
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);
}
}
}
int main() {
float** A = (float**)malloc(SIZE * sizeof(float*));
float** B = (float**)malloc(SIZE * sizeof(float*));
float** C = (float**)malloc(SIZE * sizeof(float*));
int i, j;
for (i = 0; i < SIZE; ++i) {
A[i] = (float*)malloc(SIZE * sizeof(float));
B[i] = (float*)malloc(SIZE * sizeof(float));
C[i] = (float*)malloc(SIZE * sizeof(float));
}
srand(time(0));
for (i = 0; i < SIZE; ++i) {
for (j = 0; j < SIZE; ++j) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
}
clock_t start1 = clock();
matmul(A, B, C, SIZE);
clock_t end1 = clock();
double time1 = (double)(end1 - start1) / CLOCKS_PER_SEC;
printf("tradition %f\n", time1);
clock_t start2 = clock();
matmulNEON(A, B, C, SIZE);
clock_t end2 = clock();
double time2 = (double)(end2 - start2) / CLOCKS_PER_SEC;
printf("NEON %f\n", time2);
for (i = 0; i < SIZE; i++) {
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A);
free(B);
free(C);
return 0;
}

@ -1,116 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.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 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]);
// }
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_time = clock();
sparseMatmul(&A, &B, &C);
clock_t end_time = clock();
double time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("%f\n", time);
// 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;
}

@ -1,96 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.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 free_matrix(SparseMatrix* matrix) {
free(matrix->values);
free(matrix->rowIndex);
free(matrix->colIndex);
}
int main() {
SparseMatrix A = {
.values = (int[]){1, 2, 3, 4, 5},
.rowIndex = (int[]){0, 0, 1, 2, 2},
.colIndex = (int[]){0, 2, 1, 0, 2},
.nonZeroCount = 5
};
SparseMatrix B = {
.values = (int[]){6, 8, 7, 9},
.rowIndex = (int[]){0, 2, 1, 2},
.colIndex = (int[]){0, 0, 1, 2},
.nonZeroCount = 4
};
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_time = clock();
sparseMatmul(&A, &B, &C);
clock_t end_time = clock();
double time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("%f\n", time);
int i;
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;
}

@ -1,155 +0,0 @@
#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…
Cancel
Save