ADD file via upload

main
paz4r2p6k 9 months ago
parent 761927b122
commit 339ac48183

@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 稀疏矩阵结构体
typedef struct {
float *values;
int *rowIndex;
int *colIndex;
int nonZeroCount;
} SparseMatrix;
// 稀疏矩阵乘法函数
void sparse_matmul(SparseMatrix **A, SparseMatrix **B, SparseMatrix **C) {
(*C)->nonZeroCount = 0;
int i, j, k;
for (i = 0; i < (*A)->nonZeroCount; i++) {
int rowA = (*A)->rowIndex[i];
for (j = 0; j < (*B)->nonZeroCount; j++) {
int colB = (*B)->colIndex[j];
if ((*A)->colIndex[i] == (*B)->rowIndex[j]) {
for (k = 0; k < (*C)->nonZeroCount; k++) {
if ((*C)->rowIndex[k] == rowA && (*C)->colIndex[k] == colB) {
break;
}
}
if (k < (*C)->nonZeroCount) {
(*C)->values[k] += (*A)->values[i] * (*B)->values[j];
} else {
float *new_values = (float *)realloc((*C)->values, ((*C)->nonZeroCount + 1) * sizeof(float));
int *new_rowIndex = (int *)realloc((*C)->rowIndex, ((*C)->nonZeroCount + 1) * sizeof(int));
int *new_colIndex = (int *)realloc((*C)->colIndex, ((*C)->nonZeroCount + 1) * sizeof(int));
if (new_values == NULL || new_rowIndex == NULL || new_colIndex == NULL) {
free(new_values);
free(new_rowIndex);
free(new_colIndex);
fprintf(stderr, "realloc failed in sparse_matmul function!\n");
exit(1);
}
(*C)->values = new_values;
(*C)->rowIndex = new_rowIndex;
(*C)->colIndex = new_colIndex;
(*C)->values[(*C)->nonZeroCount] = (*A)->values[i] * (*B)->values[j];
(*C)->rowIndex[(*C)->nonZeroCount] = rowA;
(*C)->colIndex[(*C)->nonZeroCount] = colB;
(*C)->nonZeroCount++;
}
}
}
}
}
int main() {
SparseMatrix A = {
.nonZeroCount = 5,
.values = (float *)malloc(5 * sizeof(float)),
.rowIndex = (int *)malloc(5 * sizeof(int)),
.colIndex = (int *)malloc(5 * sizeof(int))
};
if (A.values == NULL || A.rowIndex == NULL || A.colIndex == NULL) {
free(A.values);
free(A.rowIndex);
free(A.colIndex);
fprintf(stderr, "Memory allocation failed for matrix A!\n");
return 1;
}
A.values[0] = 1;
A.values[1] = 2;
A.values[2] = 3;
A.values[3] = 4;
A.values[4] = 5;
A.rowIndex[0] = 0;
A.rowIndex[1] = 1;
A.rowIndex[2] = 2;
A.rowIndex[3] = 1;
A.rowIndex[4] = 2;
A.colIndex[0] = 0;
A.colIndex[1] = 1;
A.colIndex[2] = 2;
A.colIndex[3] = 0;
A.colIndex[4] = 1;
SparseMatrix B = {
.nonZeroCount = 4,
.values = (float *)malloc(4 * sizeof(float)),
.rowIndex = (int *)malloc(4 * sizeof(int)),
.colIndex = (int *)malloc(4 * sizeof(int))
};
if (B.values == NULL || B.rowIndex == NULL || B.colIndex == NULL) {
free(B.values);
free(B.rowIndex);
free(B.colIndex);
fprintf(stderr, "Memory allocation failed for matrix B!\n");
return 1;
}
B.values[0] = 2;
B.values[1] = 3;
B.values[2] = 4;
B.values[3] = 5;
B.rowIndex[0] = 0;
B.rowIndex[1] = 1;
B.rowIndex[2] = 1;
B.rowIndex[3] = 2;
B.colIndex[0] = 0;
B.colIndex[1] = 1;
B.colIndex[2] = 0;
B.colIndex[3] = 1;
SparseMatrix C = {
.values = NULL,
.rowIndex = NULL,
.colIndex = NULL,
.nonZeroCount = 0
};
SparseMatrix *pA = &A;
SparseMatrix *pB = &B;
SparseMatrix *pC = &C;
clock_t start, end;
start = clock();
sparse_matmul(&pA, &pB, &pC);
end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("time: %lf s\n", time_taken);
free(A.values);
free(A.rowIndex);
free(A.colIndex);
free(B.values);
free(B.rowIndex);
free(B.colIndex);
if (C.values!= NULL) {
free(C.values);
}
if (C.rowIndex!= NULL) {
free(C.rowIndex);
}
if (C.colIndex!= NULL) {
free(C.colIndex);
}
return 0;
}
Loading…
Cancel
Save