day1: part2 complete

Removed function call in part2 hot loop. This enabled full parallelisation for part 1 and 2

Fortune for aoc2021's current commit: Small blessing − 小吉
master
Avril 3 years ago
parent c705154f09
commit 66df1b2e0a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -15,7 +15,7 @@ INPUT_DEST=src/input.c
COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
COMMON_FLAGS+=-msse -msse2 -msse3 COMMON_FLAGS+=-msse -msse2 -msse3
COMMON_FLAGS+=-D_PART1 COMMON_FLAGS+=-D_PART1 -D_PART2
OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \

@ -13,11 +13,6 @@ typedef uint16_t input_t;
extern const size_t INPUT_SIZE; extern const size_t INPUT_SIZE;
extern const input_t INPUT[]; extern const input_t INPUT[];
struct ipair {
input_t prev;
input_t next;
};
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -0,0 +1,24 @@
#ifndef _PAIRS_H
#define _PAIRS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <input.h>
struct ipair {
input_t prev;
input_t next;
};
#ifdef __cplusplus
uint64_t chk_pair(const ipair* __restrict__ pair) __attribute__((pure));
uint64_t sum_pairs(uint64_t length, const input_t input[const __restrict__ length]) __attribute__((pure));
}
#else
uint64_t chk_pair(const struct ipair* restrict pair) __attribute__((pure));
uint64_t sum_pairs(uint64_t length, const input_t input[const restrict length]) __attribute__((pure));
#endif
#endif /* _PAIRS_H */

@ -1,18 +1,27 @@
#include <input.h> #include <input.h>
#include <pairs.h>
__attribute__((pure)) __attribute__((pure))
uint64_t chk_pair(const struct ipair* restrict pair) uint64_t chk_pair(const struct ipair* restrict pair)
{ {
return pair->prev < pair->next; return pair->prev < pair->next;
} }
int part1(uint64_t* restrict _res) __attribute__((pure))
uint64_t sum_pairs(uint64_t length, const input_t input[const restrict length])
{ {
uint64_t res = 0; uint64_t res = 0;
for(size_t i=0;i<INPUT_SIZE-1;i++) for(size_t i=0;i<length-1;i++)
{ {
res += chk_pair((const struct ipair*)(INPUT+i)); res += chk_pair((const struct ipair*)(input+i));
} }
*_res = res; return res;
}
int part1(uint64_t* restrict _res)
{
*_res = sum_pairs(INPUT_SIZE, INPUT);
return 0; return 0;
} }

@ -1,11 +1,30 @@
#include <input.h> #include <input.h>
#include <tracem/macros.h> #include <pairs.h>
struct window {
input_t pre, cur, suc;
};
__attribute__((pure))
inline static input_t sum_window(const struct window* restrict w)
{
return w->pre + w->cur + w->suc;
}
int part2(uint64_t* restrict _res) int part2(uint64_t* restrict _res)
{ {
uint64_t res=0; uint64_t res=0;
TODO("unimplemented");
for(size_t i=0;i<INPUT_SIZE-2;i++)
{
struct ipair w6;
w6.prev = sum_window((const struct window*)(INPUT+i));
w6.next = sum_window((const struct window*)(INPUT+i+1));
res += (w6.prev < w6.next);
}
*_res = res; *_res = res;
return 0; return 0;
} }

Loading…
Cancel
Save