Created
August 18, 2019 04:26
-
-
Save ConnorBaker/0b55f663febf6ba0daf2e330e0f59f5f to your computer and use it in GitHub Desktop.
For a 6.7GB file, takes 2.2s. Compiled with clang -Ofast -Wall -Weverything -pedantic -std=c11 -fopenmp -flto=thin main.c.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <omp.h> | |
| #include <stdint.h> | |
| #define BUFF_SIZE 1024*1024*16 | |
| #define UCHAR_MAX 255 | |
| int main(void) { | |
| FILE *f = fopen("/home/connorbaker/storage/large_test_file-02", "r"); | |
| if (NULL == f) { | |
| printf("Well damn. File not found.\n"); | |
| exit(1); | |
| } | |
| size_t *map = (size_t *) calloc(UCHAR_MAX + 1, sizeof(size_t)); | |
| char *arr = (char *) calloc(BUFF_SIZE, sizeof(char)); | |
| size_t read; | |
| size_t iterations = 0; | |
| while ((read = fread(arr, sizeof(char), BUFF_SIZE - 1, f)) != 0) { | |
| #pragma omp parallel | |
| { | |
| size_t local_map[UCHAR_MAX + 1] = {0}; | |
| #pragma omp for | |
| for (size_t i = 0; i < read; i++) { | |
| local_map[(int) arr[i]]++; | |
| } | |
| #pragma omp critical | |
| for (int i = 0; i <= UCHAR_MAX; i++) { | |
| map[i] += local_map[i]; | |
| } | |
| } | |
| iterations++; | |
| } | |
| for (int i = 0; i <= UCHAR_MAX; i++) { | |
| if (map[i] != 0) { | |
| printf("%c = %lu\n", (char) i, map[i]); | |
| } | |
| } | |
| printf("%zu iterations.\n", iterations); | |
| fclose(f); | |
| free(map); | |
| free(arr); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment