From ef18dbe76a969cf0be4796f9d0de9c50fc418597 Mon Sep 17 00:00:00 2001 From: pypzov7ui <2137546866@qq.com> Date: Fri, 29 Nov 2024 11:11:37 +0800 Subject: [PATCH] ADD file via upload --- step5.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 step5.c diff --git a/step5.c b/step5.c new file mode 100644 index 0000000..6635537 --- /dev/null +++ b/step5.c @@ -0,0 +1,111 @@ +#include +#include +#include + +typedef struct { + int* values; + int* rowIndex; + int* colIndex; + int nonZeroCount; + int rows; + int cols; +} SparseMatrix; + +void initSparseMatrix(SparseMatrix* mat, int rows, int cols, int nonZeroCount) { + mat->rows = rows; + mat->cols = cols; + mat->nonZeroCount = nonZeroCount; + + mat->values = (int*)malloc(nonZeroCount * sizeof(int)); + mat->rowIndex = (int*)malloc(nonZeroCount * sizeof(int)); + mat->colIndex = (int*)malloc(nonZeroCount * sizeof(int)); +} + +void printSparseMatrix(SparseMatrix* mat) { + printf("Values: "); + for (int i = 0; i < mat->nonZeroCount; i++) { + printf("%d ", mat->values[i]); + } + printf("\nRow indices: "); + for (int i = 0; i < mat->nonZeroCount; i++) { + printf("%d ", mat->rowIndex[i]); + } + printf("\nColumn indices: "); + for (int i = 0; i < mat->nonZeroCount; i++) { + printf("%d ", mat->colIndex[i]); + } + printf("\n"); +} + +void sparse_matmul(SparseMatrix* A, SparseMatrix* B, int* C, int resultRows, int resultCols) { + for (int i = 0; i < resultRows * resultCols; i++) { + C[i] = 0; + } + + for (int i = 0; i < A->nonZeroCount; i++) { + int rowA = A->rowIndex[i]; + int colA = A->colIndex[i]; + int valueA = A->values[i]; + + for (int j = 0; j < B->nonZeroCount; j++) { + int rowB = B->rowIndex[j]; + int colB = B->colIndex[j]; + int valueB = B->values[j]; + + if (colA == rowB) { + C[rowA * resultCols + colB] += valueA * valueB; + } + } + } +} + +int main() { + SparseMatrix A, B; + int nonZeroCountA = 4, nonZeroCountB = 4; + + initSparseMatrix(&A, 3, 3, nonZeroCountA); + A.values[0] = 5; A.rowIndex[0] = 0; A.colIndex[0] = 0; + A.values[1] = 8; A.rowIndex[1] = 0; A.colIndex[1] = 2; + A.values[2] = 3; A.rowIndex[2] = 1; A.colIndex[2] = 1; + A.values[3] = 6; A.rowIndex[3] = 2; A.colIndex[3] = 0; + + initSparseMatrix(&B, 3, 3, nonZeroCountB); + B.values[0] = 2; B.rowIndex[0] = 0; B.colIndex[0] = 1; + B.values[1] = 7; B.rowIndex[1] = 1; B.colIndex[1] = 0; + B.values[2] = 4; B.rowIndex[2] = 2; B.colIndex[2] = 2; + B.values[3] = 9; B.rowIndex[3] = 2; B.colIndex[3] = 1; + + printf("Matrix A:\n"); + printSparseMatrix(&A); + printf("\nMatrix B:\n"); + printSparseMatrix(&B); + + int resultRows = A.rows; + int resultCols = B.cols; + int* C = (int*)malloc(resultRows * resultCols * sizeof(int)); + + clock_t start = clock(); + sparse_matmul(&A, &B, C, resultRows, resultCols); + clock_t end = clock(); + + printf("\nMatrix C (Result):\n"); + for (int i = 0; i < resultRows; i++) { + for (int j = 0; j < resultCols; j++) { + printf("%d ", C[i * resultCols + j]); + } + printf("\n"); + } + + double time_taken = (double)(end - start) / CLOCKS_PER_SEC; + printf("\nSparse Matrix Multiplication Time: %f seconds\n", time_taken); + + free(A.values); + free(A.rowIndex); + free(A.colIndex); + free(B.values); + free(B.rowIndex); + free(B.colIndex); + free(C); + + return 0; +}