aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--flake.lock27
-rw-r--r--flake.nix58
-rw-r--r--src/1/main.c13
-rwxr-xr-xsrc/2/mainbin0 -> 34312 bytes
-rw-r--r--src/2/main.c82
-rw-r--r--src/2/table7
7 files changed, 191 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..eaa8d95
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
1.envrc
2.ccls
3.ccls-cache
4.direnv
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..d24a185
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
1{
2 "nodes": {
3 "nixpkgs": {
4 "locked": {
5 "lastModified": 1633083543,
6 "narHash": "sha256-thXKms0SvDirYb8/Hw0zqAE1TsuSFWd0y51mSzkPGLU=",
7 "owner": "NixOS",
8 "repo": "nixpkgs",
9 "rev": "92609f3d9bc3acffbdbe54fa1c591a885612aa73",
10 "type": "github"
11 },
12 "original": {
13 "owner": "NixOS",
14 "ref": "nixos-21.05",
15 "repo": "nixpkgs",
16 "type": "github"
17 }
18 },
19 "root": {
20 "inputs": {
21 "nixpkgs": "nixpkgs"
22 }
23 }
24 },
25 "root": "root",
26 "version": 7
27}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..3308ce1
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,58 @@
1{
2 description = "A very basic flake";
3
4 inputs = {
5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.05";
6 };
7
8 outputs =
9 { self
10 , nixpkgs
11 }:
12 let
13 supportedSystems = [ "x86_64-linux" ];
14 forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
15
16 nixpkgsFor = forAllSystems (system:
17 import nixpkgs { inherit system; }
18 );
19
20 in
21 {
22 devShell = forAllSystems
23 (system:
24 let
25 pkgs = nixpkgsFor."${system}";
26 in
27 with pkgs;
28 mkShell {
29 nativeBuildInputs = [ gcc ];
30 });
31
32 apps = forAllSystems
33 (system:
34 let
35 pkgs = nixpkgsFor."${system}";
36 execs = with builtins; map toString [ 1 2 ];
37 mkApp = name: with pkgs; stdenv.mkDerivation {
38 name = "${name}";
39 src = ./src;
40 buildInputs = [ gcc ];
41 unpackPhase = ''
42 true
43 '';
44 buildPhase = ''
45 gcc -o main $src/${name}/main.c -fopenmp
46 '';
47 installPhase = ''
48 install -Dm755 main $out/bin/main
49 '';
50 };
51 in
52 with pkgs;
53 lib.genAttrs execs (p: {
54 type = "app";
55 program = "${mkApp p}/bin/main";
56 }));
57 };
58}
diff --git a/src/1/main.c b/src/1/main.c
new file mode 100644
index 0000000..975c495
--- /dev/null
+++ b/src/1/main.c
@@ -0,0 +1,13 @@
1#include <omp.h>
2#include <stdio.h>
3
4int main() {
5 long long int total = 1, i;
6#pragma omp parallel for reduction(*:total)
7 for(i = 1; i <= 20; i++) {
8 // printf("working in thread %d\n", omp_get_thread_num());
9 total = total * i;
10 }
11 printf("total: %lld\n", total);
12}
13
diff --git a/src/2/main b/src/2/main
new file mode 100755
index 0000000..b999f77
--- /dev/null
+++ b/src/2/main
Binary files differ
diff --git a/src/2/main.c b/src/2/main.c
new file mode 100644
index 0000000..ee7f2c8
--- /dev/null
+++ b/src/2/main.c
@@ -0,0 +1,82 @@
1#include <stdio.h>
2#include <stdlib.h>
3
4#include <omp.h>
5
6int main() {
7 int **a, **b, **c, l, m1, m2, n;
8
9 printf("Order of matrix A: ");
10 int _ = scanf("%d %d", &l, &m1);
11
12 printf("Order of matrix B: ");
13 _ = scanf("%d %d", &m2, &n);
14
15 if (m1 != m2) {
16 printf("Invalid order\n");
17 exit(1);
18 }
19
20 int m = m1;
21
22 a = (int **) calloc(l, sizeof(int *));
23 for(int i = 0; i < m; i++) a[i] = (int *) calloc(m, sizeof(int));
24
25 b = (int **) calloc(m, sizeof(int *));
26 for(int i = 0; i < n; i++) b[i] = (int *) calloc(n, sizeof(int));
27
28 c = (int **) calloc(l, sizeof(int *));
29 for(int i = 0; i < n; i++) c[i] = (int *) calloc(n, sizeof(int));
30
31
32 printf("L: %d, M: %d, N: %d\n", l, m, n);
33
34 // input for A
35 // printf("Enter A:\n");
36 for(int i = 0; i < l; i++) {
37 for(int j = 0; j < m; j++) {
38 // _ = scanf("%d", &a[i][j]);
39 a[i][j] = rand() % 500;
40 }
41 }
42
43 // input for B
44 // printf("Enter B:\n");
45 for(int i = 0; i < m; i++) {
46 for(int j = 0; j < n; j++) {
47 // _ = scanf("%d", &b[i][j]);
48 b[i][j] = rand() % 1000;
49 }
50 }
51
52 // printf("\nA: \n");
53 for(int i = 0; i < l; i++) {
54 for(int j = 0; j < m; j++) {
55 // printf("%d\t", a[i][j]);
56 }
57 // printf("\n");
58 }
59
60 // printf("\nB: \n");
61 for(int i = 0; i < m; i++) {
62 for(int j = 0; j < n; j++) {
63 // printf("%d\t", b[i][j]);
64 }
65 // printf("\n");
66 }
67
68 double start_time = omp_get_wtime();
69 int i, j, k;
70#pragma omp parallel for private(j, k) num_threads(1)
71 for(i = 0; i < l; i++) {
72 for(j = 0; j < m; j++) {
73 c[i][j] = 0;
74 for(k = 0; k < n; k++) {
75 c[i][j] += a[i][k] * b[k][j];
76 }
77 }
78 }
79 double end_time = omp_get_wtime();
80
81 printf(">>= Done in %f secs", end_time - start_time);
82}
diff --git a/src/2/table b/src/2/table
new file mode 100644
index 0000000..8e89bcc
--- /dev/null
+++ b/src/2/table
@@ -0,0 +1,7 @@
1 | 1 | 2 | 4 | 8
2-----|-----------|-----------|-----------|----------
3 100 | 0.000862 | 0.000813 | 0.000492 | 0.018057
4 250 | 0.016979 | 0.007088 | 0.008280 | 0.023153
5 500 | 0.187274 | 0.095052 | 0.083152 | 0.081629
61000 | 2.522172 | 1.221258 | 1.139272 | 1.361240
72000 | 47.200504 | 26.900284 | 19.395751 | 15.864646