From 58c3f188fedf0b511b71dd3a80c4bf541d2314f2 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Wed, 24 Aug 2022 17:21:01 +0300 Subject: [PATCH] fix makefile base (debug/release) --- Socket_CAN/ca/gen.sh | 0 Socket_CAN/diff/clientscript | 0 Socket_CAN/diff/serverscript | 0 Socket_CAN/socketcan0 | 0 cmakelists_/{Makefiledbg => Makefile} | 47 ++++---- factor.c | 144 +++++++++++++++++++++++ serialsockCANmanage/www/bin/runSpeedCtrl | 0 simple_Eratosfen_sieve.c | 19 +-- 8 files changed, 180 insertions(+), 30 deletions(-) mode change 100644 => 100755 Socket_CAN/ca/gen.sh mode change 100644 => 100755 Socket_CAN/diff/clientscript mode change 100644 => 100755 Socket_CAN/diff/serverscript mode change 100644 => 100755 Socket_CAN/socketcan0 rename cmakelists_/{Makefiledbg => Makefile} (50%) create mode 100644 factor.c mode change 100644 => 100755 serialsockCANmanage/www/bin/runSpeedCtrl diff --git a/Socket_CAN/ca/gen.sh b/Socket_CAN/ca/gen.sh old mode 100644 new mode 100755 diff --git a/Socket_CAN/diff/clientscript b/Socket_CAN/diff/clientscript old mode 100644 new mode 100755 diff --git a/Socket_CAN/diff/serverscript b/Socket_CAN/diff/serverscript old mode 100644 new mode 100755 diff --git a/Socket_CAN/socketcan0 b/Socket_CAN/socketcan0 old mode 100644 new mode 100755 diff --git a/cmakelists_/Makefiledbg b/cmakelists_/Makefile similarity index 50% rename from cmakelists_/Makefiledbg rename to cmakelists_/Makefile index 8b41010..6f1a785 100644 --- a/cmakelists_/Makefiledbg +++ b/cmakelists_/Makefile @@ -1,38 +1,43 @@ # run `make DEF=...` to add extra defines -PROGRAM := -<<<<<<< HEAD +PROGRAM := dumpregs LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all +LDFLAGS += -lusefull_macros SRCS := $(wildcard *.c) DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 OBJDIR := mk CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99 -======= -LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all -Xlinker --warn-common -SRCS := $(wildcard *.c) -DEFINES := $(DEF) -D_XOPEN_SOURCE=1111 -OBJDIR := mk -CFLAGS += -O2 -Wall -Werror -Wextra -Wno-trampolines -std=gnu99 ->>>>>>> 8c51b1c4c29737d7d3c062d88cbb0f3ca2f97652 OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) DEPS := $(OBJS:.o=.d) +TARGFILE := $(OBJDIR)/TARGET CC = gcc -#CXX = g++ +#TARGET := RELEASE +ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes) + TARGET := $(file < $(TARGFILE)) +else + TARGET := RELEASE +endif -all : $(OBJDIR) $(PROGRAM) +ifeq ($(TARGET), DEBUG) + .DEFAULT_GOAL := debug +endif + +release: $(PROGRAM) -<<<<<<< HEAD debug: CFLAGS += -DEBUG -Werror -debug: all +debug: TARGET := DEBUG +debug: $(PROGRAM) -======= ->>>>>>> 8c51b1c4c29737d7d3c062d88cbb0f3ca2f97652 -$(PROGRAM) : $(OBJS) +$(TARGFILE): $(OBJDIR) + @echo -e "\t\tTARGET: $(TARGET)" + @echo "$(TARGET)" > $(TARGFILE) + +$(PROGRAM) : $(TARGFILE) $(OBJS) @echo -e "\t\tLD $(PROGRAM)" $(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM) $(OBJDIR): - mkdir $(OBJDIR) + @mkdir $(OBJDIR) ifneq ($(MAKECMDGOALS),clean) -include $(DEPS) @@ -44,13 +49,9 @@ $(OBJDIR)/%.o: %.c clean: @echo -e "\t\tCLEAN" - @rm -f $(OBJS) $(DEPS) - @rmdir $(OBJDIR) 2>/dev/null || true + @rm -rf $(OBJDIR) 2>/dev/null || true xclean: clean @rm -f $(PROGRAM) -gentags: - CFLAGS="$(CFLAGS) $(DEFINES)" geany -g $(PROGRAM).c.tags *[hc] 2>/dev/null - -.PHONY: gentags clean xclean +.PHONY: clean xclean diff --git a/factor.c b/factor.c new file mode 100644 index 0000000..55797d9 --- /dev/null +++ b/factor.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include + +#define THREADNO 8 + +typedef struct _num{ + uint64_t n; + uint8_t isprime; + struct _num *next; + struct _num *prev; +} num; + +static num *listhead = NULL, *listtail = NULL; + +static void ins(uint64_t n, uint8_t isprime){// printf("ins %zd %d\n", n, isprime); + num *N = calloc(1, sizeof(num)); + N->n = n; N->isprime = isprime; + if(listhead){ + N->prev = listtail; + listtail->next = N; + listtail = N; + }else{ + listhead = N; + listtail = N; + } +} + +static num *del(num *el){// printf("del %zd %d\n", el->n, el->isprime); + num *nxt = el->next, *prev = el->prev; + if(el == listhead) listhead = nxt; + if(el == listtail) listtail = prev; + if(el->prev) el->prev->next = nxt; + if(el->next) el->next->prev = prev; + free(el); + return nxt; +} + +static uint64_t __1st[2] = {2,3}; +static void factorize(uint64_t n){ + if(n < 2) return; + for(int i = 0; i < 2; ++i){ + if(n % __1st[i] == 0){ + ins(__1st[i], 1); + n /= __1st[i]; + } + } + if(n < 2) return; + uint8_t prime = 1; + register uint64_t last = (2 + sqrt(n))/6; + #pragma omp parallel for shared(n, prime) + for(uint64_t i = 1; i < last; ++i){ + uint64_t nmb = i*6 - 1; + for(uint64_t x = 0; x < 3; x += 2){ + nmb += x; + if(n % nmb == 0){ + #pragma omp critical + { + ins(nmb, 0); + prime = 0; + } + }} + } + if(prime){ // prime number + ins(n, 1); + } +} + +static void calc_fact(uint64_t numb){ + num *n = NULL; + if(listhead){ // free previous list + n = listhead; + do{ + num *nxt = n->next; + free(n); n = nxt; + }while(n); + listhead = listtail = NULL; + } + factorize(numb); + n = listhead; + do{ + if(n->n == numb){ + n = del(n); + continue; + } + if(!n->isprime){ + uint64_t p = n->n; + n = del(n); + factorize(p); + continue; + } + n = n->next; + }while(n); +} + +int main(int argc, char **argv){ + if(argc != 2){ + printf("Usage: %s number or -t for test\n", argv[0]); + return 1; + } + omp_set_num_threads(THREADNO); + if(strcmp(argv[1], "-t") == 0){ // run test + uint64_t nmax = 0; double tmax = 0.; + for(uint64_t x = UINT64_MAX; x > 1ULL<<58; --x){ + double t0 = dtime(); + calc_fact(x); + double t = dtime() - t0; + if(t > tmax){ + tmax = t; + nmax = x; + printf("n = %lu, t=%g\n", nmax, tmax); + } + } + return 0; + } + uint64_t numb = atoll(argv[1]), norig = numb; + calc_fact(numb); + printf("Number: %zd\n", numb); + if(!listhead || (listhead == listtail && listhead->n == numb)){ + printf("Prime number %zd\n", numb); + return 0; + } + num *n = listhead; + printf("Fact: "); + uint64_t test = 1; + do{ + while(numb % n->n == 0){ + printf("%zd ", n->n); + numb /= n->n; + test *= n->n; + } + n = n->next; + }while(n); + if(numb != 1){ + printf("%zd", numb); + test *= numb; + } + printf("\n\nTest: %zd == %zd ?\n", norig, test); + return 0; +} diff --git a/serialsockCANmanage/www/bin/runSpeedCtrl b/serialsockCANmanage/www/bin/runSpeedCtrl old mode 100644 new mode 100755 diff --git a/simple_Eratosfen_sieve.c b/simple_Eratosfen_sieve.c index bd0d581..ab69096 100644 --- a/simple_Eratosfen_sieve.c +++ b/simple_Eratosfen_sieve.c @@ -24,9 +24,9 @@ static inline __attribute__((always_inline)) void reset(uint64_t idx){ int main(){ masks = malloc(MASKSZ); + double t0 = dtime(); memset(masks, 0b10101010, MASKSZ); // without even numbers masks[0] = 0b10101100; // with 2 and withoun 0 and 1 - double t0 = dtime(); for(uint64_t i = 3; i < 64; i += 2){ if(get(i)){ for(uint64_t j = i*2; j < MNUMBERS; j += i){ @@ -35,18 +35,23 @@ int main(){ } } printf("\tfirst 64: %g\n", dtime()-t0); - omp_set_num_threads(8); -#pragma omp parallel for - for(uint64_t i = 65; i < MNUMBERS; i += 2){ - if(get(i)){ - for(uint64_t j = i*2; j < MNUMBERS; j += i){ +#define THREADNO 8 + omp_set_num_threads(THREADNO); +#pragma omp parallel for shared(masks) + for(uint64_t i = 0; i < THREADNO; ++i){ + for(uint64_t idx = 65 + 2*omp_get_thread_num(); idx < MNUMBERS; idx += THREADNO*2){ + if(get(idx)){ + for(uint64_t j = idx*2; j < MNUMBERS; j += idx){ reset(j); } - } + }} } printf("\tfull table: %g\n", dtime()-t0); for(uint64_t i = 0; i < 1000; ++i){ if(get(i)) printf("%zd\n", i); } + for(uint64_t last = MNUMBERS-1; last > 1000; --last){ + if(get(last)){ printf("last in list: %zd\n", last); break;} + } return 0; }