diff --git a/.travis.yml b/.travis.yml index 4b2d057..e0d410e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,9 @@ language: c +arch: + - amd64 + - arm64 + os: - linux - osx diff --git a/AUTHORS b/AUTHORS index 11ba950..0b3020b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,7 +7,7 @@ dcurl is written by: Externel Source: * src/pow_sse.c is derived from preliminary work of Shinya Yagyu. * src/pow_cl.c and src/pow_kernel.cl are adopted from ccurl [1]. -* src/pow_fpga_accel.c is derived from accelerator by LampaLab [2]. +* src/pow_fpga.c is derived from accelerator by LampaLab [2]. * src/list.h is adopted from National Cheng Kung University, Taiwan. [1] https://github.com/iotaledger/ccurl diff --git a/Doxyfile b/Doxyfile index 6fe3a44..24a4a3e 100644 --- a/Doxyfile +++ b/Doxyfile @@ -2,7 +2,7 @@ # Project related configuration options #--------------------------------------------------------------------------- PROJECT_NAME = "dcurl" -PROJECT_NUMBER = 0.5.0 +PROJECT_NUMBER = 0.6.0 OUTPUT_DIRECTORY = docs OPTIMIZE_OUTPUT_FOR_C = YES #--------------------------------------------------------------------------- diff --git a/LICENSE b/LICENSE index 3d2d9ab..bb5c56e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2018-2019 BiiLabs Co., Ltd. and Contributors. +Copyright (C) 2018-2020 BiiLabs Co., Ltd. and Contributors. Copyright (C) 2018 Ievgen Korokyi. Copyright (C) 2017 IOTA AS, IOTA Foundation and Developers. Copyright (C) 2016 Shinya Yagyu. diff --git a/Makefile b/Makefile index 197b778..6f99b69 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 0.5.0 +VERSION = 0.6.0 OUT ?= ./build SRC := src @@ -22,13 +22,16 @@ endif ifneq ("$(BUILD_DEBUG)","0") CFLAGS += -Og -g3 -DENABLE_DEBUG ifneq ("$(BUILD_DEBUG)","1") - include mk/sanitizers.mk + include mk/dynamic-analysis.mk endif else # Enable all the valid optimizations for standard programs in release build CFLAGS += -O3 endif +# Static code analysis +include mk/static-analysis.mk + # Check specific CPU features available on build host include mk/cpu-features.mk @@ -72,7 +75,7 @@ include mk/opencl.mk endif ifeq ("$(BUILD_FPGA_ACCEL)","1") -CFLAGS += -DENABLE_FPGA_ACCEL +CFLAGS += -DENABLE_FPGA endif ifeq ("$(BUILD_REMOTE)","1") @@ -137,17 +140,17 @@ endif ifeq ("$(BUILD_JNI)","1") OBJS += \ - jni/iri-pearldiver-exlib.o + jni/iri_pearldiver_exlib.o endif ifeq ("$(BUILD_COMPAT)", "1") OBJS += \ - compat-ccurl.o + compat_ccurl.o endif ifeq ("$(BUILD_FPGA_ACCEL)","1") OBJS += \ - pow_fpga_accel.o + pow_fpga.o endif ifeq ("$(BUILD_REMOTE)", "1") @@ -157,7 +160,7 @@ OBJS += \ WORKER_EXCLUDE_OBJS := remote_interface.o ifeq ("$(BUILD_JNI)", "1") -WORKER_EXCLUDE_OBJS += jni/iri-pearldiver-exlib.o +WORKER_EXCLUDE_OBJS += jni/iri_pearldiver_exlib.o endif WORKER_OBJS := $(addprefix $(OUT)/worker-,$(filter-out $(WORKER_EXCLUDE_OBJS), $(OBJS))) WORKER_CFLAGS := $(filter-out -DENABLE_REMOTE, $(CFLAGS)) diff --git a/README.md b/README.md index a444256..3372dcb 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,43 @@ IOTA Reference Implementation (IRI) adaptation is available to benefit from hard ## Build Instructions Check [docs/build-n-test.md](docs/build-n-test.md) for details. +## Source Code Naming Convention +Check [docs/naming-convention.md](docs/naming-convention.md) for details. + ## Performance -After integrating dcurl into IRI, performance of [attachToTangle](https://iota.readme.io/reference#attachtotangle) is measured as following. -* Each sampling is measured with 30 transaction trytes and total 200 samples are measured. -* mwm = 14, 26 CPU threads to find nonce -* Settings: enable 2 pow tasks in CPU, 1 pow tasks in GPU at the same time +After integrating dcurl into IRI, performance of [attachToTangle](https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#attachtotangle) is measured as the following. + +* Setting: MWM = 14, 200 attachToTangle API requests with each containing 2 transactions +* Local CPU: + * AMD Ryzen Threadripper 2990WX 32-Core Processor + * 2 PoW tasks at the same time + * Each task uses 32 CPU threads to find nonce + * SIMD enabled +* Remote worker: + * The board with Intel/Altera Cyclone V SoC + * 1 PoW task at the same time in a board + * FPGA acceleration enabled + * Connected with local network + ![](https://github.com/raw/DLTcollab/dcurl/develop/docs/benchmark.png) +### Conclusion + +Except the original IRI, the other instances use the [DLTcollab/IRI](https://github.com/DLTcollab/iri) instead of [iotaledger/IRI](https://github.com/iotaledger/iri). + +| IRI version | attachToTangle API behavior | Effect | +|:-|:-|:-| +| IOTA IRI | One transaction bundle at the same time **(Synchronized)** | Transactions of a bundle are calculated one by one | +| DLTCollab IRI | Multiple transaction bundles at the same time | Transactions of different bundles compete for the PoW calculation resources | + +The original IRI should be the slowest one since it does not contain any PoW acceleration. +However, the graph is different from the expectation. +This is caused by 2 factors: +* The graph shows the execution time of each API request instead of the overall throughput. +* The table shows that there are competition of the PoW resources, which means the execution time would be longer than expected. + +And from the graph we can see that 4 remote workers would be a good choice to accelerate PoW. + ## IRI Adaptation [Modified IRI accepting external PoW Library](https://github.com/DLTcollab/iri) Supported IRI version: 1.7.0 diff --git a/cppcheck_suppress b/cppcheck_suppress new file mode 100644 index 0000000..0b5795e --- /dev/null +++ b/cppcheck_suppress @@ -0,0 +1,2 @@ +// Do not treat system header files missing as errors +missingIncludeSystem:* diff --git a/docs/benchmark.png b/docs/benchmark.png index 12ebd62..1f5edf3 100644 Binary files a/docs/benchmark.png and b/docs/benchmark.png differ diff --git a/docs/board-de10-nano.md b/docs/board-de10-nano.md index bc9e77c..5c2db11 100644 --- a/docs/board-de10-nano.md +++ b/docs/board-de10-nano.md @@ -25,7 +25,7 @@ Change the FPGA configuration mode switch as shown in the image\ -## Connect with the De10-Nano board +## Connect with the DE10-Nano board ### Hardware Please make sure the following list are well set or connected: - 5V DC Power Jack @@ -92,6 +92,23 @@ $ sudo minicom ``` Then enter the user account and the password to login. +### MAC address setting (optional) +For monitoring or using [Ansible Playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks.html) on remote workers, +the IP address of them should be fixed. +The router can assign the IP address by detecting the hardware MAC address. +However, each time the **DE10-Nano** board is rebooted, the MAC address would be different. + +The steps to set the MAC address: +- Open the file `/etc/network/interfaces` +- Add the following text + ``` + auto eth0 + iface eth0 inet dhcp + hwaddress ether xx:xx:xx:xx:xx:xx + ``` + xx:xx:xx:xx:xx:xx is the assigned MAC address +- Reboot + ## Build and execute the remote worker ### Load the driver diff --git a/docs/build-n-test.md b/docs/build-n-test.md index ce2e615..9dc1d1b 100644 --- a/docs/build-n-test.md +++ b/docs/build-n-test.md @@ -132,6 +132,11 @@ Success. [ Verified ] ``` +## Static Code Analysis +``` +$ make static-analysis +``` + ## Tweaks * Number of threads to find nonce in CPU * ```$ export DCURL_NUM_CPU=26``` diff --git a/docs/naming-convention.md b/docs/naming-convention.md new file mode 100644 index 0000000..663e7df --- /dev/null +++ b/docs/naming-convention.md @@ -0,0 +1,27 @@ +# Naming Convention + +dcurl uses the **[snake case](https://en.wikipedia.org/wiki/Snake_case)** naming convention. + +- Variable and function + ``` + bool pow_c(void *pow_ctx) { + ...... + int completed_index = -1; + ...... + } + ``` + +- Structure: The suffixes **_s** represents for structure and **_t** represents for type + ``` + typedef struct pwork_s pwork_t; + struct pwork_s { + ...... + } + ``` + +- Macro: Use capital letters + ``` + #define MIN_TRYTE_VALUE (-13) + #define MAX_TRYTE_VALUE 13 + ``` + diff --git a/docs/remote-interface.md b/docs/remote-interface.md index 557385d..d8c5dc0 100644 --- a/docs/remote-interface.md +++ b/docs/remote-interface.md @@ -77,3 +77,17 @@ $ make BUILD_REMOTE=1 BUILD_DEBUG=1 check ## Requirements Remote interface requires RabbitMQ broker + +## Fallback mechanism +If the remote interface is not working for some reason, the PoW calculation will be transferred to the local hardwares. +The possible situations are: + +* Initialization failure: +The remote interface is not initialized successfully because the RabbitMQ broker it not activated. +dcurl would record the initialization status of the remote interface. +If it does not succeed, dcurl would use local hardwares to do the PoW. + +* Runtime failure: +The remote interface is initialized successfully but the remote workers do not exist or the RabbitMQ broker is closed after initialization. +dcurl would wait 10 seconds for the responding. +If nothing returns, dcurl would use local hardwares to do the PoW. diff --git a/jni/iri-pearldiver-exlib.c b/jni/iri-pearldiver-exlib.c deleted file mode 100644 index fe0bf3c..0000000 --- a/jni/iri-pearldiver-exlib.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2018-2019 BiiLabs Co., Ltd. and Contributors - * All Rights Reserved. - * This is free software; you can redistribute it and/or modify it under the - * terms of the MIT license. A copy of the license can be found in the file - * "LICENSE" at the root of this distribution. - */ - -#include "iri-pearldiver-exlib.h" -#include -#include -#include "../src/dcurl.h" -#include "../src/trinary.h" - -JNIEXPORT jboolean JNICALL -Java_com_iota_iri_crypto_PearlDiver_exlibInit(JNIEnv *env, jclass clazz) -{ - if (!dcurl_init()) - return JNI_FALSE; - return JNI_TRUE; -} - -JNIEXPORT jboolean JNICALL -Java_com_iota_iri_crypto_PearlDiver_exlibSearch(JNIEnv *env, - jclass clazz, - jbyteArray trits, - jint mwm, - jint threads) -{ - /*********** Get the Byte array from Java byte Array *************/ - jbyte *c_trits = (*env)->GetByteArrayElements(env, trits, NULL); - - Trits_t *arg_trits = initTrits((int8_t *) c_trits, 8019); - Trytes_t *arg_trytes = trytes_from_trits(arg_trits); - if (!arg_trytes) - return JNI_FALSE; - /****************************************************************/ - - int8_t *result = dcurl_entry(arg_trytes->data, mwm, threads); - - /************ Write result back Java byte array *****************/ - Trytes_t *ret_trytes = initTrytes(result, 2673); - Trits_t *ret_trits = trits_from_trytes(ret_trytes); - if (!ret_trits) - return JNI_FALSE; - (*env)->SetByteArrayRegion(env, trits, 0, 8019, ret_trits->data); - /****************************************************************/ - - free(result); - freeTrobject(ret_trytes); - freeTrobject(arg_trits); - freeTrobject(arg_trytes); - freeTrobject(ret_trits); - - return JNI_TRUE; -} - -JNIEXPORT void JNICALL -Java_com_iota_iri_crypto_PearlDiver_exlibCancel(JNIEnv *env, jclass clazz) -{ - /* Do nothing */ -} - -JNIEXPORT void JNICALL -Java_com_iota_iri_crypto_PearlDiver_exlibDestroy(JNIEnv *env, jclass clazz) -{ - dcurl_destroy(); -} diff --git a/jni/iri_pearldiver_exlib.c b/jni/iri_pearldiver_exlib.c new file mode 100644 index 0000000..076982d --- /dev/null +++ b/jni/iri_pearldiver_exlib.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2018-2019 BiiLabs Co., Ltd. and Contributors + * All Rights Reserved. + * This is free software; you can redistribute it and/or modify it under the + * terms of the MIT license. A copy of the license can be found in the file + * "LICENSE" at the root of this distribution. + */ + +#include "iri_pearldiver_exlib.h" +#include +#include +#include "../src/dcurl.h" +#include "../src/trinary.h" + +JNIEXPORT jboolean JNICALL +Java_com_iota_iri_crypto_PearlDiver_exlibInit(JNIEnv *env, + jclass clazz, + jbyteArray broker_host, + jint broker_host_len) +{ + jboolean ret = JNI_TRUE; + + /* Get the Byte array from Java byte Array */ + jbyte *host = (*env)->GetByteArrayElements(env, broker_host, NULL); + + dcurl_config config; + config.broker_host = malloc(broker_host_len + 1); + if (!config.broker_host) { + ret = JNI_FALSE; + goto fail; + } + memcpy(config.broker_host, (char *) host, broker_host_len); + config.broker_host[broker_host_len] = '\0'; + + if (!dcurl_init(&config)) + ret = JNI_FALSE; + + free(config.broker_host); + +fail: + return ret; +} + +JNIEXPORT jboolean JNICALL +Java_com_iota_iri_crypto_PearlDiver_exlibSearch(JNIEnv *env, + jclass clazz, + jbyteArray trits, + jint mwm, + jint threads) +{ + jboolean ret = JNI_TRUE; + + /* Get the Byte array from Java byte Array */ + jbyte *c_trits = (*env)->GetByteArrayElements(env, trits, NULL); + + trits_t *arg_trits = init_trits((int8_t *) c_trits, 8019); + trytes_t *arg_trytes = trytes_from_trits(arg_trits); + if (!arg_trytes) { + ret = JNI_FALSE; + goto fail_input; + } + + int8_t *result = dcurl_entry(arg_trytes->data, mwm, threads); + + /* Write result back Java byte array */ + trytes_t *ret_trytes = init_trytes(result, 2673); + trits_t *ret_trits = trits_from_trytes(ret_trytes); + if (!ret_trits) { + ret = JNI_FALSE; + goto fail_output; + } + (*env)->SetByteArrayRegion(env, trits, 0, 8019, ret_trits->data); + +fail_output: + free(result); + free_trinary_object(ret_trits); + free_trinary_object(ret_trytes); +fail_input: + free_trinary_object(arg_trits); + free_trinary_object(arg_trytes); + + return ret; +} + +JNIEXPORT void JNICALL +Java_com_iota_iri_crypto_PearlDiver_exlibCancel(JNIEnv *env, jclass clazz) +{ + /* Do nothing */ +} + +JNIEXPORT void JNICALL +Java_com_iota_iri_crypto_PearlDiver_exlibDestroy(JNIEnv *env, jclass clazz) +{ + dcurl_destroy(); +} diff --git a/mk/sanitizers.mk b/mk/dynamic-analysis.mk similarity index 100% rename from mk/sanitizers.mk rename to mk/dynamic-analysis.mk diff --git a/mk/java.mk b/mk/java.mk index b8c6812..d77122e 100644 --- a/mk/java.mk +++ b/mk/java.mk @@ -41,7 +41,7 @@ $(OUT)/com/iota/iri/crypto/PearlDiver.java: $(OUT)/com/iota/iri/crypto $(Q)$(CURL) -s -o $@ \ "https://github.com/raw/$(GITHUB_REPO)/$(PearlDriverSRC)" -$(OUT)/jni/iri-pearldiver-exlib.h: $(OUT)/com/iota/iri/crypto/PearlDiver.java +$(OUT)/jni/iri_pearldiver_exlib.h: $(OUT)/com/iota/iri/crypto/PearlDiver.java $(VECHO) " JAVAH\t$@\n" $(Q)$(JAVAH) -classpath $(OUT) -o $@ com.iota.iri.crypto.PearlDiver @@ -56,7 +56,7 @@ endif CFLAGS_JNI += -I$(JAVA_HOME)/include CFLAGS_JNI += -I$(OUT)/jni -jni/iri-pearldiver-exlib.c: $(OUT)/jni/iri-pearldiver-exlib.h +jni/iri_pearldiver_exlib.c: $(OUT)/jni/iri_pearldiver_exlib.h $(OUT)/jni/%.o: jni/%.c $(VECHO) " CC\t$@\n" diff --git a/mk/static-analysis.mk b/mk/static-analysis.mk new file mode 100644 index 0000000..4c40206 --- /dev/null +++ b/mk/static-analysis.mk @@ -0,0 +1,52 @@ +# Copy from mk/java.mk for Infer static code analysis, +# since Infer needs the environment variable JAVA_HOME to be set +# If JAVA_HOME is not set, guess it according to system configurations +ifndef JAVA_HOME + JAVAC := $(shell which javac) + ifeq ($(UNAME_S),Darwin) + # macOS + JAVA_HOME := $(shell /usr/libexec/java_home) + else + # Linux + JAVA_HOME := $(shell readlink -f $(JAVAC) | sed "s:/bin/javac::") + endif +endif # JAVA_HOME + +static-analysis: + # CppCheck + cppcheck \ + --enable=all \ + --error-exitcode=1 \ + --force \ + --inline-suppr \ + -I $(SRC) \ + $(LIBTUV_INCLUDE) \ + $(LIBRABBITMQ_INCLUDE) \ + $(SSE2NEON_INCLUDE) \ + --quiet \ + --suppressions-list=cppcheck_suppress \ + -UERANGE \ + $(SRC) tests + # Infer + $(MAKE) distclean + $(MAKE) BUILD_JNI=1 # Workaround: For enabling Infer to check the build option BUILD_JNI=1 + $(MAKE) distclean + JAVA_HOME=$(JAVA_HOME) infer run \ + --fail-on-issue \ + --skip-analysis-in-path +deps/rabbitmq-c/librabbitmq/amqp* \ + --skip-analysis-in-path +deps/rabbitmq-c/tests/test* \ + -- \ + $(MAKE) \ + BUILD_AVX=1 \ + BUILD_GPU=1 \ + BUILD_FPGA_ACCEL=1 \ + BUILD_REMOTE=1 \ + BUILD_JNI=1 \ + BUILD_STAT=1 + $(MAKE) distclean + infer run --fail-on-issue -- $(MAKE) \ + BUILD_SSE=1 + $(MAKE) distclean + infer run --fail-on-issue -- $(MAKE) \ + BUILD_GENERIC=1 + $(MAKE) distclean diff --git a/src/clcontext.c b/src/clcontext.c index c817740..b7fded7 100644 --- a/src/clcontext.c +++ b/src/clcontext.c @@ -13,7 +13,7 @@ #include "constants.h" #include "pearl.cl.h" -static bool init_cl_devices(CLContext *ctx) +static bool init_cl_devices(cl_context_t *ctx) { cl_int errno; @@ -40,14 +40,14 @@ static bool init_cl_devices(CLContext *ctx) ctx->num_work_group = 1; /* Create Command Queue */ - ctx->cmdq = clCreateCommandQueue(ctx->context, ctx->device, 0, &errno); + ctx->cmd_q = clCreateCommandQueue(ctx->context, ctx->device, 0, &errno); if (errno != CL_SUCCESS) return false; /* Failed to create command queue */ return true; } -static bool init_cl_program(CLContext *ctx) +static bool init_cl_program(cl_context_t *ctx) { unsigned char *source_str = pearl_cl; size_t source_size = pearl_cl_len; @@ -67,7 +67,7 @@ static bool init_cl_program(CLContext *ctx) return true; } -static bool init_cl_kernel(CLContext *ctx) +static bool init_cl_kernel(cl_context_t *ctx) { char *kernel_name[] = {"init", "search", "finalize"}; cl_int errno; @@ -80,9 +80,9 @@ static bool init_cl_kernel(CLContext *ctx) return true; } -static bool init_cl_buffer(CLContext *ctx) +static bool init_cl_buffer(cl_context_t *ctx) { - cl_ulong mem = 0, max_mem = 0; + cl_ulong mem, max_mem = 0; cl_int errno; for (int i = 0; i < ctx->kernel_info.num_buffers; i++) { @@ -118,31 +118,31 @@ static bool init_cl_buffer(CLContext *ctx) return true; } -static bool init_BufferInfo(CLContext *ctx) +static bool init_buffer_info(cl_context_t *ctx) { ctx->kernel_info.buffer_info[INDEX_OF_TRIT_HASH] = - (BufferInfo){sizeof(char) * HASH_TRITS_LENGTH, CL_MEM_WRITE_ONLY}; - ctx->kernel_info.buffer_info[INDEX_OF_MID_LOW] = (BufferInfo){ + (buffer_info_t){sizeof(char) * HASH_TRITS_LENGTH, CL_MEM_WRITE_ONLY}; + ctx->kernel_info.buffer_info[INDEX_OF_MID_LOW] = (buffer_info_t){ sizeof(int64_t) * STATE_TRITS_LENGTH, CL_MEM_READ_WRITE, 2}; - ctx->kernel_info.buffer_info[INDEX_OF_MID_HIGH] = (BufferInfo){ + ctx->kernel_info.buffer_info[INDEX_OF_MID_HIGH] = (buffer_info_t){ sizeof(int64_t) * STATE_TRITS_LENGTH, CL_MEM_READ_WRITE, 2}; - ctx->kernel_info.buffer_info[INDEX_OF_STATE_LOW] = (BufferInfo){ + ctx->kernel_info.buffer_info[INDEX_OF_STATE_LOW] = (buffer_info_t){ sizeof(int64_t) * STATE_TRITS_LENGTH, CL_MEM_READ_WRITE, 2}; - ctx->kernel_info.buffer_info[INDEX_OF_STATE_HIGH] = (BufferInfo){ + ctx->kernel_info.buffer_info[INDEX_OF_STATE_HIGH] = (buffer_info_t){ sizeof(int64_t) * STATE_TRITS_LENGTH, CL_MEM_READ_WRITE, 2}; ctx->kernel_info.buffer_info[INDEX_OF_MWM] = - (BufferInfo){sizeof(size_t), CL_MEM_READ_ONLY}; + (buffer_info_t){sizeof(size_t), CL_MEM_READ_ONLY}; ctx->kernel_info.buffer_info[INDEX_OF_FOUND] = - (BufferInfo){sizeof(char), CL_MEM_READ_WRITE}; + (buffer_info_t){sizeof(char), CL_MEM_READ_WRITE}; ctx->kernel_info.buffer_info[INDEX_OF_NONCE_PROBE] = - (BufferInfo){sizeof(int64_t), CL_MEM_READ_WRITE, 2}; + (buffer_info_t){sizeof(int64_t), CL_MEM_READ_WRITE, 2}; ctx->kernel_info.buffer_info[INDEX_OF_LOOP_COUNT] = - (BufferInfo){sizeof(size_t), CL_MEM_READ_ONLY}; + (buffer_info_t){sizeof(size_t), CL_MEM_READ_ONLY}; return init_cl_buffer(ctx); } -static bool set_clcontext(CLContext *ctx, cl_device_id device) +static bool set_clcontext(cl_context_t *ctx, cl_device_id device) { ctx->device = device; ctx->kernel_info.num_buffers = 9; @@ -152,12 +152,12 @@ static bool set_clcontext(CLContext *ctx, cl_device_id device) return init_cl_devices(ctx) && init_cl_program(ctx); } -int init_clcontext(CLContext *ctx) +int init_clcontext(cl_context_t *ctx) { int ctx_idx = 0; cl_uint num_platform = 0; - cl_platform_id *platform = NULL; + cl_platform_id *platform; /* Get the platform */ clGetPlatformIDs(0, NULL, &num_platform); @@ -167,7 +167,7 @@ int init_clcontext(CLContext *ctx) clGetPlatformIDs(num_platform, platform, NULL); cl_uint num_devices = 0; - cl_device_id *devices = NULL; + cl_device_id *devices; /* Iterate the platform list and get its devices */ for (int i = 0; i < num_platform; i++) { @@ -181,7 +181,7 @@ int init_clcontext(CLContext *ctx) int ret = 1; ret &= set_clcontext(&ctx[ctx_idx], devices[j]); ret &= init_cl_kernel(&ctx[ctx_idx]); - ret &= init_BufferInfo(&ctx[ctx_idx]); + ret &= init_buffer_info(&ctx[ctx_idx]); if (!ret) { free(devices); goto leave; diff --git a/src/clcontext.h b/src/clcontext.h index 434bf7c..770a9da 100644 --- a/src/clcontext.h +++ b/src/clcontext.h @@ -23,19 +23,19 @@ typedef struct { size_t size; cl_mem_flags flags; size_t init_flags; -} BufferInfo; +} buffer_info_t; typedef struct { - BufferInfo buffer_info[MAX_NUM_BUFFERS]; + buffer_info_t buffer_info[MAX_NUM_BUFFERS]; size_t num_buffers; size_t num_kernels; size_t num_src; -} KernelInfo; +} kernel_info_t; -/* Every GPU device has own CLContext */ +/* Every GPU device has its own cl_context_t */ typedef struct { cl_device_id device; - cl_command_queue cmdq; + cl_command_queue cmd_q; cl_mem buffer[MAX_NUM_BUFFERS]; cl_kernel kernel[MAX_NUM_KERNEL]; cl_program program; @@ -43,9 +43,9 @@ typedef struct { cl_uint num_cores; cl_ulong max_memory; size_t num_work_group; - KernelInfo kernel_info; + kernel_info_t kernel_info; uint64_t hash_count; -} CLContext; +} cl_context_t; enum { INDEX_OF_TRIT_HASH, @@ -66,7 +66,7 @@ enum { }; /* return the number of available device */ -int init_clcontext(CLContext *ctx); +int init_clcontext(cl_context_t *ctx); #define KERNEL_PATH "./src/pow_kernel.cl" diff --git a/src/common.h b/src/common.h index 9288133..6cd2b1e 100644 --- a/src/common.h +++ b/src/common.h @@ -15,12 +15,18 @@ #include #define __DCURL_MAJOR__ 0 -#define __DCURL_MINOR__ 5 +#define __DCURL_MINOR__ 6 #define __DCURL_PATCH__ 0 double diff_in_second(struct timespec t1, struct timespec t2); -static inline void ddprintf(const char *format, ...) +/* Copy from logger project: + * https://bitbucket.org/embear/logger/src/abef6b0a6c991545a3d3fecfbc39d2b0448fb85a/include/logger.h#lines-199*/ +typedef int16_t logger_id_t; + +static inline void log_debug(logger_id_t const logger_id, + const char *format, + ...) { #if defined(ENABLE_DEBUG) va_list ap; @@ -31,9 +37,20 @@ static inline void ddprintf(const char *format, ...) #endif } -typedef struct _pow_info PoW_Info; +static inline void log_info(logger_id_t const logger_id, + const char *format, + ...) +{ + va_list ap; + va_start(ap, format); + vprintf(format, ap); + va_end(ap); + fflush(stdout); +} + +typedef struct pow_info_s pow_info_t; -struct _pow_info { +struct pow_info_s { double time; uint64_t hash_count; }; diff --git a/src/compat-ccurl.c b/src/compat_ccurl.c similarity index 67% rename from src/compat-ccurl.c rename to src/compat_ccurl.c index 423de15..d914986 100644 --- a/src/compat-ccurl.c +++ b/src/compat_ccurl.c @@ -10,27 +10,30 @@ #include #include "dcurl.h" -static bool isInitialized = false; +static bool is_initialized = false; /* mutex protecting initialization section */ static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; +// cppcheck-suppress unusedFunction ; The unused functions are for the ccurl compatibility char *ccurl_pow(char *trytes, int mwm) { pthread_mutex_lock(&mtx); - if (!isInitialized) { + if (!is_initialized) { dcurl_init(); - isInitialized = true; + is_initialized = true; } pthread_mutex_unlock(&mtx); return (char *) dcurl_entry((int8_t *) trytes, mwm, 1); } +// cppcheck-suppress unusedFunction ; The unused functions are for the ccurl compatibility void ccurl_pow_finalize(void) { dcurl_destroy(); } +// cppcheck-suppress unusedFunction ; The unused functions are for the ccurl compatibility void ccurl_pow_interrupt(void) { /* Do Nothing */ diff --git a/src/constants.c b/src/constants.c index 668e12b..93035c2 100644 --- a/src/constants.c +++ b/src/constants.c @@ -6,7 +6,7 @@ * "LICENSE" at the root of this distribution. */ -char TryteAlphabet[] = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +char tryte_alphabet[] = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int indices[] = { 0, 364, 728, 363, 727, 362, 726, 361, 725, 360, 724, 359, 723, 358, 722, diff --git a/src/constants.h b/src/constants.h index 751eb48..3e12130 100644 --- a/src/constants.h +++ b/src/constants.h @@ -9,11 +9,11 @@ #ifndef CONSTANTS_H_ #define CONSTANTS_H_ -#define MinTryteValue -13 -#define MaxTryteValue 13 -#define SignatureSize 6561 -#define Depth 3 -#define Radix 3 +#define MIN_TRYTE_VALUE (-13) +#define MAX_TRYTE_VALUE 13 +#define SIGNATURE_SIZE 6561 +#define DEPTH 3 +#define RADIX 3 #define HASH_TRYTES_LENGTH 81 #define NONCE_TRYTES_LENGTH 27 @@ -25,60 +25,64 @@ #define STATE_TRITS_LENGTH 3 * HASH_TRITS_LENGTH #define TRANSACTION_TRITS_LENGTH (TRANSACTION_TRYTES_LENGTH * 3) -#define SignatureMessageFragmentTrinaryOffset 0 -#define SignatureMessageFragmentTrinarySize 6561 -#define AddressTrinaryOffset \ - (SignatureMessageFragmentTrinaryOffset + \ - SignatureMessageFragmentTrinarySize) -#define AddressTrinarySize 243 -#define ValueTrinaryOffset (AddressTrinaryOffset + AddressTrinarySize) -#define ValueTrinarySize 81 -#define ObsoleteTagTrinaryOffset (ValueTrinaryOffset + ValueTrinarySize) -#define ObsoleteTagTrinarySize 81 -#define TimestampTrinaryOffset \ - (ObsoleteTagTrinaryOffset + ObsoleteTagTrinarySize) -#define TimestampTrinarySize 27 -#define CurrentIndexTrinaryOffset \ - (TimestampTrinaryOffset + TimestampTrinarySize) -#define CurrentIndexTrinarySize 27 -#define LastIndexTrinaryOffset \ - (CurrentIndexTrinaryOffset + CurrentIndexTrinarySize) -#define LastIndexTrinarySize 27 -#define BundleTrinaryOffset (LastIndexTrinaryOffset + LastIndexTrinarySize) -#define BundleTrinarySize 243 -#define TrunkTransactionTrinaryOffset (BundleTrinaryOffset + BundleTrinarySize) -#define TrunkTransactionTrinarySize 243 -#define BranchTransactionTrinaryOffset \ - (TrunkTransactionTrinaryOffset + TrunkTransactionTrinarySize) -#define BranchTransactionTrinarySize 243 -#define TagTrinaryOffset \ - (BranchTransactionTrinaryOffset + BranchTransactionTrinarySize) -#define TagTrinarySize 81 -#define AttachmentTimestampTrinaryOffset (TagTrinaryOffset + TagTrinarySize) -#define AttachmentTimestampTrinarySize 27 +#define SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET 0 +#define SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE 6561 +#define ADDRESS_TRINARY_OFFSET \ + (SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET + \ + SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE) +#define ADDRESS_TRINARY_SIZE 243 +#define VALUE_TRINARY_OFFSET (ADDRESS_TRINARY_OFFSET + ADDRESS_TRINARY_SIZE) +#define VALUE_TRINARY_SIZE 81 +#define OBSOLETE_TAG_TRINARY_OFFSET (VALUE_TRINARY_OFFSET + VALUE_TRINARY_SIZE) +#define OBSOLETE_TAG_TRINARY_SIZE 81 +#define TIMESTAMP_TRINARY_OFFSET \ + (OBSOLETE_TAG_TRINARY_OFFSET + OBSOLETE_TAG_TRINARY_SIZE) +#define TIMESTAMP_TRINARY_SIZE 27 +#define CURRENT_INDEX_TRINARY_OFFSET \ + (TIMESTAMP_TRINARY_OFFSET + TIMESTAMP_TRINARY_SIZE) +#define CURRENT_INDEX_TRINARY_SIZE 27 +#define LAST_INDEX_TRINARY_OFFSET \ + (CURRENT_INDEX_TRINARY_OFFSET + CURRENT_INDEX_TRINARY_SIZE) +#define LAST_INDEX_TRINARY_SIZE 27 +#define BUNDLE_TRINARY_OFFSET \ + (LAST_INDEX_TRINARY_OFFSET + LAST_INDEX_TRINARY_SIZE) +#define BUNDLE_TRINARY_SIZE 243 +#define TRUNK_TRANSACTION_TRINARY_OFFSET \ + (BUNDLE_TRINARY_OFFSET + BUNDLE_TRINARY_SIZE) +#define TRUNK_TRANSACTION_TRINARY_SIZE 243 +#define BRANCH_TRANSACTION_TRINARY_OFFSET \ + (TRUNK_TRANSACTION_TRINARY_OFFSET + TRUNK_TRANSACTION_TRINARY_SIZE) +#define BRANCH_TRANSACTION_TRINARY_SIZE 243 +#define TAG_TRINARY_OFFSET \ + (BRANCH_TRANSACTION_TRINARY_OFFSET + BRANCH_TRANSACTION_TRINARY_SIZE) +#define TAG_TRINARY_SIZE 81 +#define ATTACHMENT_TIMESTAMP_TRINARY_OFFSET \ + (TAG_TRINARY_OFFSET + TAG_TRINARY_SIZE) +#define ATTACHMENT_TIMESTAMP_TRINARY_SIZE 27 -#define AttachmentTimestampLowerBoundTrinaryOffset \ - (AttachmentTimestampTrinaryOffset + AttachmentTimestampTrinarySize) -#define AttachmentTimestampLowerBoundTrinarySize 27 -#define AttachmentTimestampUpperBoundTrinaryOffset \ - (AttachmentTimestampLowerBoundTrinaryOffset + \ - AttachmentTimestampLowerBoundTrinarySize) -#define AttachmentTimestampUpperBoundTrinarySize 27 -#define NonceTrinaryOffset \ - (AttachmentTimestampUpperBoundTrinaryOffset + \ - AttachmentTimestampUpperBoundTrinarySize) -#define NonceTrinarySize 81 +#define ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_OFFSET \ + (ATTACHMENT_TIMESTAMP_TRINARY_OFFSET + ATTACHMENT_TIMESTAMP_TRINARY_SIZE) +#define ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_SIZE 27 +#define ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_OFFSET \ + (ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_OFFSET + \ + ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_SIZE) +#define ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_SIZE 27 +#define NONCE_TRINARY_OFFSET \ + (ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_OFFSET + \ + ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_SIZE) +#define NONCE_TRINARY_SIZE 81 -#define transactionTrinarySize \ - (SignatureMessageFragmentTrinarySize + AddressTrinarySize + \ - ValueTrinarySize + ObsoleteTagTrinarySize + TimestampTrinarySize + \ - CurrentIndexTrinarySize + LastIndexTrinarySize + BundleTrinarySize + \ - TrunkTransactionTrinarySize + BranchTransactionTrinarySize + \ - TagTrinarySize + AttachmentTimestampTrinarySize + \ - AttachmentTimestampLowerBoundTrinarySize + \ - AttachmentTimestampUpperBoundTrinarySize + NonceTrinarySize) +#define TRANSACTION_TRINARY_SIZE \ + (SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE + ADDRESS_TRINARY_SIZE + \ + VALUE_TRINARY_SIZE + OBSOLETE_TAG_TRINARY_SIZE + TIMESTAMP_TRINARY_SIZE + \ + CURRENT_INDEX_TRINARY_SIZE + LAST_INDEX_TRINARY_SIZE + \ + BUNDLE_TRINARY_SIZE + TRUNK_TRANSACTION_TRINARY_SIZE + \ + BRANCH_TRANSACTION_TRINARY_SIZE + TAG_TRINARY_SIZE + \ + ATTACHMENT_TIMESTAMP_TRINARY_SIZE + \ + ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_SIZE + \ + ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_SIZE + NONCE_TRINARY_SIZE) -extern char TryteAlphabet[]; +extern char tryte_alphabet[]; extern const int indices[]; #endif diff --git a/src/cpu-utils.h b/src/cpu-utils.h index 754fdcb..5df381b 100644 --- a/src/cpu-utils.h +++ b/src/cpu-utils.h @@ -46,9 +46,9 @@ static unsigned int get_nprocs_conf() */ static inline int get_nthds_per_phys_proc() { - FILE *fd; int nthread; #if defined(__linux__) + FILE *fd; char nthd[4]; fd = popen("LC_ALL=C lscpu | grep 'Thread(s) per core' | awk '{printf $4}'", @@ -61,7 +61,11 @@ static inline int get_nthds_per_phys_proc() if (errno == ERANGE || nthread == 0) { return -1; } + + if (pclose(fd) == -1) + return -1; #elif defined(__APPLE__) + FILE *fd; char p_proc[4], l_proc[4]; int phys_proc, logic_proc; @@ -85,10 +89,12 @@ static inline int get_nthds_per_phys_proc() } nthread = logic_proc / phys_proc; -#endif if (pclose(fd) == -1) return -1; +#else + nthread = 1; +#endif return nthread; } @@ -107,33 +113,30 @@ static inline int get_nthds_per_phys_proc() static inline int get_avail_phys_nprocs() { int nthd; - size_t nproc; + size_t nproc = 0; + char *env_ncpu; nthd = get_nthds_per_phys_proc(); + env_ncpu = getenv("DCURL_NUM_CPU"); if (nthd <= 0) return -1; - else + else if (env_ncpu) { + do { + char *end; + signed int num = strtol(env_ncpu, &end, 10); + if (end == env_ncpu) { + /* if no characters were converted these pointers are equal */ + break; + } + if (errno == ERANGE || num > INT_MAX || num < 0) { + /* because strtol produces a long, check for overflow */ + break; + } + nproc = num; + } while (0); + } else nproc = (get_nprocs_conf() / nthd) - 1; - do { - char *env_ncpu = getenv("DCURL_NUM_CPU"); - if (!env_ncpu) { - break; - } - - char *end; - signed int num = strtol(env_ncpu, &end, 10); - if (end == env_ncpu) { - /* if no characters were converted these pointers are equal */ - break; - } - if (errno == ERANGE || num > INT_MAX || num < 0) { - /* because strtol produces a long, check for overflow */ - break; - } - nproc = num; - } while (0); - if (!nproc) nproc = 1; return nproc; diff --git a/src/curl.c b/src/curl.c index bc18a59..921f7d6 100644 --- a/src/curl.c +++ b/src/curl.c @@ -11,18 +11,18 @@ #include #include -static const int8_t truthTable[11] = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0}; +static const int8_t truth_table[11] = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0}; static void _transform(int8_t state[]) { - int r = 0, i = 0; + int r, i; int8_t copy[STATE_TRITS_LENGTH] = {0}; int8_t *from = state, *to = copy; for (r = 0; r < 81; r++) { for (i = 0; i < STATE_TRITS_LENGTH; i++) { int aa = indices[i]; int bb = indices[i + 1]; - to[i] = truthTable[from[aa] + (from[bb] * 4) + 5]; + to[i] = truth_table[from[aa] + (from[bb] * 4) + 5]; } int8_t *tmp = from; from = to; @@ -31,16 +31,19 @@ static void _transform(int8_t state[]) memcpy(state, copy, STATE_TRITS_LENGTH); } -void Transform(Curl *c) +void transform(curl_t *c) { _transform(c->state->data); } -void Absorb(Curl *c, Trytes_t *inn) +void absorb(curl_t *c, trytes_t *inn) { - Trits_t *in = trits_from_trytes(inn); + trits_t *in = trits_from_trytes(inn); int lenn = 0; + if (!in) + return; + for (int i = 0; i < in->len; i += lenn) { lenn = 243; if (in->len - i < 243) @@ -49,43 +52,43 @@ void Absorb(Curl *c, Trytes_t *inn) /* Copy in[i, i + lenn] to c->state->data[0, lenn] */ memcpy(c->state->data, in->data + i, lenn); - Transform(c); + transform(c); } - freeTrobject(in); + free_trinary_object(in); } -Trytes_t *Squeeze(Curl *c) +trytes_t *squeeze(curl_t *c) { int8_t src[HASH_TRITS_LENGTH] = {0}; /* Get trits[:HASH_TRITS_LENGTH] to an array */ memcpy(src, c->state->data, HASH_TRITS_LENGTH); - Trits_t *trits = initTrits(src, HASH_TRITS_LENGTH); - Trytes_t *trytes = trytes_from_trits(trits); + trits_t *trits = init_trits(src, HASH_TRITS_LENGTH); + trytes_t *trytes = trytes_from_trits(trits); - Transform(c); - freeTrobject(trits); + transform(c); + free_trinary_object(trits); return trytes; } -Curl *initCurl() +curl_t *init_curl() { - Curl *c = (Curl *) malloc(sizeof(Curl)); + curl_t *c = (curl_t *) malloc(sizeof(curl_t)); if (!c) return NULL; int8_t src[STATE_TRITS_LENGTH] = {0}; - c->state = initTrits(src, STATE_TRITS_LENGTH); + c->state = init_trits(src, STATE_TRITS_LENGTH); return c; } -void freeCurl(Curl *c) +void free_curl(curl_t *c) { if (c) { - freeTrobject(c->state); + free_trinary_object(c->state); free(c); } } diff --git a/src/curl.h b/src/curl.h index c431855..d9f1a21 100644 --- a/src/curl.h +++ b/src/curl.h @@ -12,15 +12,15 @@ #include "constants.h" #include "trinary.h" -typedef struct _curl { - Trits_t *state; -} Curl; +typedef struct curl_s { + trits_t *state; +} curl_t; -void Absorb(Curl *c, Trytes_t *inn); -void Transform(Curl *c); -Trytes_t *Squeeze(Curl *c); +void absorb(curl_t *c, trytes_t *inn); +void transform(curl_t *c); +trytes_t *squeeze(curl_t *c); -Curl *initCurl(); -void freeCurl(Curl *c); +curl_t *init_curl(); +void free_curl(curl_t *c); #endif diff --git a/src/dcurl.c b/src/dcurl.c index fae5388..c56e01d 100644 --- a/src/dcurl.c +++ b/src/dcurl.c @@ -14,8 +14,8 @@ #if defined(ENABLE_OPENCL) #include "pow_cl.h" #endif -#if defined(ENABLE_FPGA_ACCEL) -#include "pow_fpga_accel.h" +#if defined(ENABLE_FPGA) +#include "pow_fpga.h" #endif #if defined(ENABLE_REMOTE) #include "remote_interface.h" @@ -31,76 +31,121 @@ #include "pow_c.h" #endif +/* for checking whether the corresponding implementation is initialized */ +enum capability { + CAP_NONE = 0U, + CAP_C = 1U, + CAP_SSE = 1U << 1, + CAP_AVX = 1U << 2, + CAP_GPU = 1U << 3, + CAP_FPGA = 1U << 4, + CAP_REMOTE = 1U << 5 +}; + /* check whether dcurl is initialized */ -static bool isInitialized = false; +static bool is_initialized = false; +static uint8_t runtime_caps = CAP_NONE; static uv_sem_t notify; -LIST_HEAD(IMPL_LIST); +LIST_HEAD(impl_list); +LIST_HEAD(remote_impl_list); #if defined(ENABLE_AVX) -extern ImplContext PoWAVX_Context; +extern impl_context_t pow_avx_context; #elif defined(ENABLE_SSE) -extern ImplContext PoWSSE_Context; +extern impl_context_t pow_sse_context; #elif defined(ENABLE_GENERIC) -extern ImplContext PoWC_Context; +extern impl_context_t pow_c_context; #endif #if defined(ENABLE_OPENCL) -extern ImplContext PoWCL_Context; +extern impl_context_t pow_cl_context; #endif -#if defined(ENABLE_FPGA_ACCEL) -extern ImplContext PoWFPGAAccel_Context; +#if defined(ENABLE_FPGA) +extern impl_context_t pow_fpga_context; #endif #if defined(ENABLE_REMOTE) -extern RemoteImplContext Remote_Context; +extern remote_impl_context_t remote_context; static uv_sem_t notify_remote; #endif -bool dcurl_init() +bool dcurl_init(dcurl_config *config) { - bool ret = true; + bool ret = false; #if defined(ENABLE_AVX) - ret &= registerImplContext(&PoWAVX_Context); + if (register_impl_context(&pow_avx_context)) { + runtime_caps |= CAP_AVX; + ret |= true; + } #elif defined(ENABLE_SSE) - ret &= registerImplContext(&PoWSSE_Context); + if (register_impl_context(&pow_sse_context)) { + runtime_caps |= CAP_SSE; + ret |= true; + } #elif defined(ENABLE_GENERIC) - ret &= registerImplContext(&PoWC_Context); + if (register_impl_context(&pow_c_context)) { + runtime_caps |= CAP_C; + ret |= true; + } #endif #if defined(ENABLE_OPENCL) - ret &= registerImplContext(&PoWCL_Context); + if (register_impl_context(&pow_cl_context)) { + runtime_caps |= CAP_GPU; + ret |= true; + } #endif -#if defined(ENABLE_FPGA_ACCEL) - ret &= registerImplContext(&PoWFPGAAccel_Context); +#if defined(ENABLE_FPGA) + if (register_impl_context(&pow_fpga_context)) { + runtime_caps |= CAP_FPGA; + ret |= true; + } #endif #if defined(ENABLE_REMOTE) - ret &= initializeRemoteContext(&Remote_Context); + if (!config) { + remote_context.broker_host = DEFAULT_BROKER_HOST; + } else { + remote_context.broker_host = config->broker_host; + } + if (register_remote_context(&remote_context)) { + runtime_caps |= CAP_REMOTE; + ret |= true; + } uv_sem_init(¬ify_remote, 0); #endif uv_sem_init(¬ify, 0); - return isInitialized = ret; + return is_initialized = ret; } void dcurl_destroy() { - ImplContext *impl = NULL; + impl_context_t *impl = NULL; struct list_head *p; #if defined(ENABLE_REMOTE) - destroyRemoteContext(&Remote_Context); + remote_impl_context_t *remote_impl = NULL; + struct list_head *p_remote; + + list_for_each (p_remote, &remote_impl_list) { + remote_impl = list_entry(p_remote, remote_impl_context_t, node); + destroy_remote_context(remote_impl); + list_del(p_remote); + } #endif - list_for_each (p, &IMPL_LIST) { - impl = list_entry(p, ImplContext, list); - destroyImplContext(impl); + list_for_each (p, &impl_list) { + impl = list_entry(p, impl_context_t, node); + destroy_impl_context(impl); list_del(p); } + + runtime_caps = CAP_NONE; } @@ -109,47 +154,55 @@ int8_t *dcurl_entry(int8_t *trytes, int mwm, int threads) void *pow_ctx = NULL; int8_t *res = NULL; - ImplContext *impl = NULL; + impl_context_t *impl = NULL; struct list_head *p; - if (!isInitialized) + if (!is_initialized) return NULL; #if defined(ENABLE_REMOTE) - do { - if (enterRemoteContext(&Remote_Context)) { - pow_ctx = getRemoteContext(&Remote_Context, trytes, mwm); - goto remote_pow; + if (runtime_caps & CAP_REMOTE) { + remote_impl_context_t *remote_impl = NULL; + struct list_head *p_remote; + + do { + list_for_each (p_remote, &remote_impl_list) { + remote_impl = list_entry(p_remote, remote_impl_context_t, node); + if (enter_remote_context(remote_impl)) { + pow_ctx = get_remote_context(remote_impl, trytes, mwm); + goto remote_pow; + } + } + uv_sem_wait(¬ify_remote); + } while (1); + + remote_pow: + if (!do_remote_context(remote_impl, pow_ctx)) { + /* The remote interface can not work without activated RabbitMQ + * broker and remote worker. If it is not working, the PoW would be + * calculated by the local machine. And the remote interface + * resource should be released. + */ + free_remote_context(remote_impl, pow_ctx); + exit_remote_context(remote_impl); + uv_sem_post(¬ify_remote); + goto local_pow; + } else { + res = get_remote_result(remote_impl, pow_ctx); } - uv_sem_wait(¬ify_remote); - } while (1); - -remote_pow: - if (!doRemoteContext(&Remote_Context, pow_ctx)) { - /* The remote interface can not work without activated RabbitMQ broker - * and remote worker. If it is not working, the PoW would be calculated - * by the local machine. And the remote interface resource should be - * released. - */ - freeRemoteContext(&Remote_Context, pow_ctx); - exitRemoteContext(&Remote_Context); + free_remote_context(remote_impl, pow_ctx); + exit_remote_context(remote_impl); uv_sem_post(¬ify_remote); - goto local_pow; - } else { - res = getRemoteResult(&Remote_Context, pow_ctx); + return res; } - freeRemoteContext(&Remote_Context, pow_ctx); - exitRemoteContext(&Remote_Context); - uv_sem_post(¬ify_remote); - return res; local_pow: #endif do { - list_for_each (p, &IMPL_LIST) { - impl = list_entry(p, ImplContext, list); - if (enterImplContext(impl)) { - pow_ctx = getPoWContext(impl, trytes, mwm, threads); + list_for_each (p, &impl_list) { + impl = list_entry(p, impl_context_t, node); + if (enter_impl_context(impl)) { + pow_ctx = get_pow_context(impl, trytes, mwm, threads); goto do_pow; } } @@ -157,13 +210,13 @@ int8_t *dcurl_entry(int8_t *trytes, int mwm, int threads) } while (1); do_pow: - if (!doThePoW(impl, pow_ctx)) { + if (!do_the_pow(impl, pow_ctx)) { res = NULL; } else { - res = getPoWResult(impl, pow_ctx); + res = get_pow_result(impl, pow_ctx); } - freePoWContext(impl, pow_ctx); - exitImplContext(impl); + free_pow_context(impl, pow_ctx); + exit_impl_context(impl); uv_sem_post(¬ify); return res; } diff --git a/src/dcurl.h b/src/dcurl.h index f5b67e5..3145cf3 100644 --- a/src/dcurl.h +++ b/src/dcurl.h @@ -23,16 +23,32 @@ * easily. */ +/*! The default value of the broker hostname */ +#define DEFAULT_BROKER_HOST "localhost" + +/** + * A structure representing the configuration of the initialization. + */ +typedef struct { + char *broker_host; /**< The broker hostname used in the remote mode */ +} dcurl_config; + /** * @brief dcurl initialization. * * Register the determined hardware into the list and initialize the * corresponding resource. + * @param [in] config + * @parblock + * The configuration of the initialization. + * + * NULL: Use default configuration. + * @endparblock * @return - * - true: initialization succeeded. - * - false: initialization failed. + * - true: one of the initialization succeeded. + * - false: all the initialization failed. */ -bool dcurl_init(); +bool dcurl_init(dcurl_config *config); /** * @brief dcurl destruction. @@ -53,7 +69,7 @@ void dcurl_destroy(); * @parblock * The thread number of calculating the PoW. It affects CPU only. * - * 0: use (maximum threads - 1). + * 0: use (maximum physical CPU - 1). * @endparblock * @return The result of PoW. */ diff --git a/src/implcontext.c b/src/implcontext.c index 999bac3..46c80c6 100644 --- a/src/implcontext.c +++ b/src/implcontext.c @@ -11,30 +11,33 @@ #define MSG_PREFIX "[dcurl] " -extern struct list_head IMPL_LIST; +extern struct list_head impl_list; -bool registerImplContext(ImplContext *impl_ctx) +bool register_impl_context(impl_context_t *impl_ctx) { - list_add(&impl_ctx->list, &IMPL_LIST); - return initializeImplContext(impl_ctx); + bool res = initialize_impl_context(impl_ctx); + if (res) + list_add(&impl_ctx->node, &impl_list); + return res; } -bool initializeImplContext(ImplContext *impl_ctx) +bool initialize_impl_context(impl_context_t *impl_ctx) { bool res = impl_ctx->initialize(impl_ctx); if (res) { - ddprintf(MSG_PREFIX "Implementation %s is initialized successfully\n", - impl_ctx->description); + log_debug(0, + MSG_PREFIX "Implementation %s is initialized successfully\n", + impl_ctx->description); } return res; } -void destroyImplContext(ImplContext *impl_ctx) +void destroy_impl_context(impl_context_t *impl_ctx) { return impl_ctx->destroy(impl_ctx); } -bool enterImplContext(ImplContext *impl_ctx) +bool enter_impl_context(impl_context_t *impl_ctx) { uv_mutex_lock(&impl_ctx->lock); if (impl_ctx->num_working_thread >= impl_ctx->num_max_thread) { @@ -46,34 +49,37 @@ bool enterImplContext(ImplContext *impl_ctx) return true; /* Access Success */ } -void exitImplContext(ImplContext *impl_ctx) +void exit_impl_context(impl_context_t *impl_ctx) { uv_mutex_lock(&impl_ctx->lock); impl_ctx->num_working_thread--; uv_mutex_unlock(&impl_ctx->lock); } -void *getPoWContext(ImplContext *impl_ctx, int8_t *trytes, int mwm, int threads) +void *get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { - return impl_ctx->getPoWContext(impl_ctx, trytes, mwm, threads); + return impl_ctx->get_pow_context(impl_ctx, trytes, mwm, threads); } -bool doThePoW(ImplContext *impl_ctx, void *pow_ctx) +bool do_the_pow(impl_context_t *impl_ctx, void *pow_ctx) { - return impl_ctx->doThePoW(pow_ctx); + return impl_ctx->do_the_pow(pow_ctx); } -bool freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +bool free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { - return impl_ctx->freePoWContext(impl_ctx, pow_ctx); + return impl_ctx->free_pow_context(impl_ctx, pow_ctx); } -int8_t *getPoWResult(ImplContext *impl_ctx, void *pow_ctx) +int8_t *get_pow_result(impl_context_t *impl_ctx, void *pow_ctx) { - return impl_ctx->getPoWResult(pow_ctx); + return impl_ctx->get_pow_result(pow_ctx); } -PoW_Info getPoWInfo(ImplContext *impl_ctx, void *pow_ctx) +pow_info_t get_pow_info(impl_context_t *impl_ctx, void *pow_ctx) { - return impl_ctx->getPoWInfo(pow_ctx); + return impl_ctx->get_pow_info(pow_ctx); } diff --git a/src/implcontext.h b/src/implcontext.h index 65495e7..8b230dc 100644 --- a/src/implcontext.h +++ b/src/implcontext.h @@ -15,9 +15,9 @@ #include "list.h" #include "uv.h" -typedef struct _impl_context ImplContext; +typedef struct impl_context_s impl_context_t; -struct _impl_context { +struct impl_context_s { void *context; char *description; @@ -28,34 +28,34 @@ struct _impl_context { int num_working_thread; /* Functions of Implementation Context */ - bool (*initialize)(ImplContext *impl_ctx); - void (*destroy)(ImplContext *impl_ctx); + bool (*initialize)(impl_context_t *impl_ctx); + void (*destroy)(impl_context_t *impl_ctx); /* Private PoW Context for each thread */ - void *(*getPoWContext)(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads); - bool (*doThePoW)(void *pow_ctx); - int8_t *(*getPoWResult)(void *pow_ctx); - PoW_Info (*getPoWInfo)(void *pow_ctx); - bool (*freePoWContext)(ImplContext *impl_ctx, void *pow_ctx); - - /* Linked list */ - struct list_head list; + void *(*get_pow_context)(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads); + bool (*do_the_pow)(void *pow_ctx); + int8_t *(*get_pow_result)(void *pow_ctx); + pow_info_t (*get_pow_info)(void *pow_ctx); + bool (*free_pow_context)(impl_context_t *impl_ctx, void *pow_ctx); + + /* Node in linked list */ + struct list_head node; }; -bool registerImplContext(ImplContext *impl_ctx); -bool initializeImplContext(ImplContext *impl_ctx); -void destroyImplContext(ImplContext *impl_ctx); -bool enterImplContext(ImplContext *impl_ctx); -void exitImplContext(ImplContext *impl_ctx); -void *getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads); -bool doThePoW(ImplContext *impl_ctx, void *pow_ctx); -bool freePoWContext(ImplContext *impl_ctx, void *pow_ctx); -int8_t *getPoWResult(ImplContext *impl_ctx, void *pow_ctx); -PoW_Info getPoWInfo(ImplContext *impl_ctx, void *pow_ctx); +bool register_impl_context(impl_context_t *impl_ctx); +bool initialize_impl_context(impl_context_t *impl_ctx); +void destroy_impl_context(impl_context_t *impl_ctx); +bool enter_impl_context(impl_context_t *impl_ctx); +void exit_impl_context(impl_context_t *impl_ctx); +void *get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads); +bool do_the_pow(impl_context_t *impl_ctx, void *pow_ctx); +bool free_pow_context(impl_context_t *impl_ctx, void *pow_ctx); +int8_t *get_pow_result(impl_context_t *impl_ctx, void *pow_ctx); +pow_info_t get_pow_info(impl_context_t *impl_ctx, void *pow_ctx); #endif diff --git a/src/list.h b/src/list.h index 8630c46..1f03281 100644 --- a/src/list.h +++ b/src/list.h @@ -79,7 +79,7 @@ struct list_head { #define LIST_HEAD(head) struct list_head head = {&(head), &(head)} /** - * INIT_LIST_HEAD() - Initialize empty list head + * init_list_head() - Initialize empty list head * @head: pointer to list head * * This can also be used to initialize a unlinked list node. @@ -93,7 +93,7 @@ struct list_head { * list_del(_init) on an uninitialized node is undefined (unrelated memory is * modified, crashes, ...). */ -static inline void INIT_LIST_HEAD(struct list_head *head) +static inline void init_list_head(struct list_head *head) { head->next = head; head->prev = head; @@ -169,7 +169,7 @@ static inline void list_del(struct list_head *node) static inline void list_del_init(struct list_head *node) { list_del(node); - INIT_LIST_HEAD(node); + init_list_head(node); } /** @@ -263,7 +263,7 @@ static inline void list_splice_init(struct list_head *list, struct list_head *head) { list_splice(list, head); - INIT_LIST_HEAD(list); + init_list_head(list); } /** @@ -282,7 +282,7 @@ static inline void list_splice_tail_init(struct list_head *list, struct list_head *head) { list_splice_tail(list, head); - INIT_LIST_HEAD(list); + init_list_head(list); } /** @@ -307,7 +307,7 @@ static inline void list_cut_position(struct list_head *head_to, return; if (head_from == node) { - INIT_LIST_HEAD(head_to); + init_list_head(head_to); return; } diff --git a/src/pow_avx.c b/src/pow_avx.c index d928530..8c7216a 100644 --- a/src/pow_avx.c +++ b/src/pow_avx.c @@ -16,7 +16,7 @@ #include "implcontext.h" #ifdef __AVX2__ -static void transform256(__m256i *lmid, __m256i *hmid) +static void transform_256(__m256i *lmid, __m256i *hmid) { __m256i one = _mm256_set_epi64x(HBITS, HBITS, HBITS, HBITS); int t1, t2; @@ -56,7 +56,7 @@ static void transform256(__m256i *lmid, __m256i *hmid) } } -static int incr256(__m256i *mid_low, __m256i *mid_high) +static int incr_256(__m256i *mid_low, __m256i *mid_high) { int i; __m256i carry = _mm256_set_epi64x(LOW00, LOW01, LOW02, LOW03); @@ -72,7 +72,7 @@ static int incr256(__m256i *mid_low, __m256i *mid_high) return i == HASH_TRITS_LENGTH; } -static void seri256(__m256i *low, __m256i *high, int n, int8_t *r) +static void seri_256(__m256i *low, __m256i *high, int n, int8_t *r) { int index = n >> 6; n = n % 64; @@ -93,7 +93,7 @@ static void seri256(__m256i *low, __m256i *high, int n, int8_t *r) } } -static int check256(__m256i *l, __m256i *h, int m) +static int check_256(__m256i *l, __m256i *h, int m) { __m256i nonce_probe = _mm256_set_epi64x(HBITS, HBITS, HBITS, HBITS); __m256i one = _mm256_set_epi64x(HBITS, HBITS, HBITS, HBITS); @@ -118,7 +118,7 @@ static int check256(__m256i *l, __m256i *h, int m) return -2; } -static void para256(int8_t in[], __m256i l[], __m256i h[]) +static void para_256(int8_t in[], __m256i l[], __m256i h[]) { for (int i = 0; i < STATE_TRITS_LENGTH; i++) { switch (in[i]) { @@ -138,7 +138,7 @@ static void para256(int8_t in[], __m256i l[], __m256i h[]) } } -static void incrN256(int n, __m256i *mid_low, __m256i *mid_high) +static void incr_n_256(int n, __m256i *mid_low, __m256i *mid_high) { __m256i one = _mm256_set_epi64x(HBITS, HBITS, HBITS, HBITS); for (int j = 0; j < n; j++) { @@ -155,27 +155,28 @@ static void incrN256(int n, __m256i *mid_low, __m256i *mid_high) } -static int loop256(__m256i *lmid, - __m256i *hmid, - int m, - int8_t *nonce, - int *stopPoW, - uv_rwlock_t *lock) +static int loop_256(__m256i *lmid, + __m256i *hmid, + int m, + int8_t *nonce, + int *stop_pow, + uv_rwlock_t *lock) { - int i = 0, n = 0; + int i = 0; __m256i lcpy[STATE_TRITS_LENGTH * 2], hcpy[STATE_TRITS_LENGTH * 2]; uv_rwlock_rdlock(lock); - for (i = 0; !incr256(lmid, hmid) && !*stopPoW; i++) { + for (i = 0; !incr_256(lmid, hmid) && !*stop_pow; i++) { uv_rwlock_rdunlock(lock); + int n; for (int j = 0; j < STATE_TRITS_LENGTH; j++) { lcpy[j] = lmid[j]; hcpy[j] = hmid[j]; } - transform256(lcpy, hcpy); - if ((n = check256(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, - m)) >= 0) { - seri256(lmid, hmid, n, nonce); + transform_256(lcpy, hcpy); + if ((n = check_256(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, + m)) >= 0) { + seri_256(lmid, hmid, n, nonce); return i * 256; } uv_rwlock_rdlock(lock); @@ -184,16 +185,16 @@ static int loop256(__m256i *lmid, return -i * 256 - 1; } -static int64_t pwork256(int8_t mid[], - int mwm, - int8_t nonce[], - int n, - int *stopPoW, - uv_rwlock_t *lock) +static int64_t pwork_256(int8_t mid[], + int mwm, + int8_t nonce[], + int n, + int *stop_pow, + uv_rwlock_t *lock) { __m256i lmid[STATE_TRITS_LENGTH], hmid[STATE_TRITS_LENGTH]; int offset = HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH; - para256(mid, lmid, hmid); + para_256(mid, lmid, hmid); lmid[offset] = _mm256_set_epi64x(LOW00, LOW01, LOW02, LOW03); hmid[offset] = _mm256_set_epi64x(HIGH00, HIGH01, HIGH02, HIGH03); lmid[offset + 1] = _mm256_set_epi64x(LOW10, LOW11, LOW12, LOW13); @@ -206,14 +207,14 @@ static int64_t pwork256(int8_t mid[], hmid[offset + 4] = _mm256_set_epi64x(HIGH40, HIGH41, HIGH42, HIGH43); lmid[offset + 5] = _mm256_set_epi64x(LOW50, LOW51, LOW52, LOW53); hmid[offset + 5] = _mm256_set_epi64x(HIGH50, HIGH51, HIGH52, HIGH53); - incrN256(n, lmid, hmid); + incr_n_256(n, lmid, hmid); - return loop256(lmid, hmid, mwm, nonce, stopPoW, lock); + return loop_256(lmid, hmid, mwm, nonce, stop_pow, lock); } #else /* AVX1 */ -static void transform256(__m256d *lmid, __m256d *hmid) +static void transform_256(__m256d *lmid, __m256d *hmid) { __m256d one = _mm256_set_pd(HBITS, HBITS, HBITS, HBITS); int j, r, t1, t2; @@ -253,7 +254,7 @@ static void transform256(__m256d *lmid, __m256d *hmid) } } -static int incr256(__m256d *mid_low, __m256d *mid_high) +static int incr_256(__m256d *mid_low, __m256d *mid_high) { int i; __m256d carry; @@ -268,7 +269,7 @@ static int incr256(__m256d *mid_low, __m256d *mid_high) return i == HASH_TRITS_LENGTH; } -static void seri256(__m256d *low, __m256d *high, int n, int8_t *r) +static void seri_256(__m256d *low, __m256d *high, int n, int8_t *r) { int i = 0, index = 0; if (n > 63 && n < 128) { @@ -301,7 +302,7 @@ static void seri256(__m256d *low, __m256d *high, int n, int8_t *r) } } -static int check256(__m256d *l, __m256d *h, int m) +static int check_256(__m256d *l, __m256d *h, int m) { int i, j; // omit init for speed @@ -317,6 +318,7 @@ static int check256(__m256d *l, __m256d *h, int m) for (j = 0; j < 4; j++) { for (i = 0; i < 64; i++) { long long np = ((dl) nonce_probe[j]).l; + // cppcheck-suppress shiftTooManyBitsSigned ; The most significant bit is not used as a signed bit if ((np >> i) & 1) { return i + j * 64; } @@ -325,7 +327,7 @@ static int check256(__m256d *l, __m256d *h, int m) return -2; } -static void para256(int8_t in[], __m256d l[], __m256d h[]) +static void para_256(int8_t in[], __m256d l[], __m256d h[]) { int i = 0; for (i = 0; i < STATE_TRITS_LENGTH; i++) { @@ -346,7 +348,7 @@ static void para256(int8_t in[], __m256d l[], __m256d h[]) } } -static void incrN256(int n, __m256d *mid_low, __m256d *mid_high) +static void incr_n_256(int n, __m256d *mid_low, __m256d *mid_high) { int i, j; for (j = 0; j < n; j++) { @@ -362,27 +364,28 @@ static void incrN256(int n, __m256d *mid_low, __m256d *mid_high) } } -static int loop256(__m256d *lmid, - __m256d *hmid, - int m, - int8_t *nonce, - int *stopPoW, - uv_rwlock_t *lock) +static int loop_256(__m256d *lmid, + __m256d *hmid, + int m, + int8_t *nonce, + int *stop_pow, + uv_rwlock_t *lock) { - int i = 0, n = 0, j = 0; + int i = 0; __m256d lcpy[STATE_TRITS_LENGTH * 2], hcpy[STATE_TRITS_LENGTH * 2]; uv_rwlock_rdlock(lock); - for (i = 0; !incr256(lmid, hmid) && !*stopPoW; i++) { + for (i = 0; !incr_256(lmid, hmid) && !*stop_pow; i++) { uv_rwlock_rdunlock(lock); + int n, j; for (j = 0; j < STATE_TRITS_LENGTH; j++) { lcpy[j] = lmid[j]; hcpy[j] = hmid[j]; } - transform256(lcpy, hcpy); - if ((n = check256(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, - m)) >= 0) { - seri256(lmid, hmid, n, nonce); + transform_256(lcpy, hcpy); + if ((n = check_256(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, + m)) >= 0) { + seri_256(lmid, hmid, n, nonce); return i * 256; } uv_rwlock_rdlock(lock); @@ -391,16 +394,16 @@ static int loop256(__m256d *lmid, return -i * 256 - 1; } -static long long int pwork256(int8_t mid[], - int mwm, - int8_t nonce[], - int n, - int *stopPoW, - uv_rwlock_t *lock) +static long long int pwork_256(int8_t mid[], + int mwm, + int8_t nonce[], + int n, + int *stop_pow, + uv_rwlock_t *lock) { __m256d lmid[STATE_TRITS_LENGTH], hmid[STATE_TRITS_LENGTH]; int offset = HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH; - para256(mid, lmid, hmid); + para_256(mid, lmid, hmid); lmid[offset] = _mm256_set_pd(LOW00, LOW01, LOW02, LOW03); hmid[offset] = _mm256_set_pd(HIGH00, HIGH01, HIGH02, HIGH03); lmid[offset + 1] = _mm256_set_pd(LOW10, LOW11, LOW12, LOW13); @@ -413,36 +416,36 @@ static long long int pwork256(int8_t mid[], hmid[offset + 4] = _mm256_set_pd(HIGH40, HIGH41, HIGH42, HIGH43); lmid[offset + 5] = _mm256_set_pd(LOW50, LOW51, LOW52, LOW53); hmid[offset + 5] = _mm256_set_pd(HIGH50, HIGH51, HIGH52, HIGH53); - incrN256(n, lmid, hmid); + incr_n_256(n, lmid, hmid); - return loop256(lmid, hmid, mwm, nonce, stopPoW, lock); + return loop_256(lmid, hmid, mwm, nonce, stop_pow, lock); } #endif /* __AVX2__ */ static void work_cb(uv_work_t *req) { - Pwork_struct *pworkInfo = (Pwork_struct *) req->data; - pworkInfo->ret = - pwork256(pworkInfo->mid, pworkInfo->mwm, pworkInfo->nonce, pworkInfo->n, - pworkInfo->stopPoW, pworkInfo->lock); - - uv_rwlock_wrlock(pworkInfo->lock); - if (pworkInfo->ret >= 0) { - *pworkInfo->stopPoW = 1; + pwork_t *pwork_info = (pwork_t *) req->data; + pwork_info->ret = + pwork_256(pwork_info->mid, pwork_info->mwm, pwork_info->nonce, + pwork_info->n, pwork_info->stop_pow, pwork_info->lock); + + uv_rwlock_wrlock(pwork_info->lock); + if (pwork_info->ret >= 0) { + *pwork_info->stop_pow = 1; /* This means this thread got the result */ - pworkInfo->n = -1; + pwork_info->n = -1; } - uv_rwlock_wrunlock(pworkInfo->lock); + uv_rwlock_wrunlock(pwork_info->lock); } -static int8_t *tx_to_cstate(Trytes_t *tx) +static int8_t *tx_to_cstate(trytes_t *tx) { - Trytes_t *inn = NULL; - Trits_t *tr = NULL; + trytes_t *inn = NULL; + trits_t *tr = NULL; int8_t tyt[TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] = {0}; - Curl *c = initCurl(); + curl_t *c = init_curl(); int8_t *c_state = (int8_t *) malloc(STATE_TRITS_LENGTH); if (!c || !c_state) goto fail; @@ -450,11 +453,11 @@ static int8_t *tx_to_cstate(Trytes_t *tx) /* Copy tx->data[:TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] to tyt */ memcpy(tyt, tx->data, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); - inn = initTrytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); + inn = init_trytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); if (!inn) goto fail; - Absorb(c, inn); + absorb(c, inn); tr = trits_from_trytes(tx); if (!tr) @@ -470,19 +473,19 @@ static int8_t *tx_to_cstate(Trytes_t *tx) c->state->len - tr->len + (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH)); - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); return c_state; fail: - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); free(c_state); return NULL; } -static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) +static void nonce_to_result(trytes_t *tx, trytes_t *nonce, int8_t *ret) { int rst_len = tx->len - NONCE_TRYTES_LENGTH + nonce->len; @@ -491,26 +494,26 @@ static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) rst_len - (tx->len - NONCE_TRYTES_LENGTH)); } -static bool PowAVX(void *pow_ctx) +static bool pow_avx(void *pow_ctx) { bool res = true; - Trits_t *nonce_trit = NULL; - Trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; + trits_t *nonce_trit = NULL; + trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; struct timespec start_time, end_time; /* Initialize the context */ - PoW_AVX_Context *ctx = (PoW_AVX_Context *) pow_ctx; - ctx->stopPoW = 0; + pow_avx_context_t *ctx = (pow_avx_context_t *) pow_ctx; + ctx->stop_pow = 0; ctx->pow_info.time = 0; ctx->pow_info.hash_count = 0; uv_rwlock_init(&ctx->lock); uv_loop_t *loop_ptr = &ctx->loop; uv_work_t *work_req = ctx->work_req; - Pwork_struct *pitem = ctx->pitem; + pwork_t *pitem = ctx->pitem; int8_t **nonce_array = ctx->nonce_array; /* Prepare the input trytes for algorithm */ - tx_tryte = initTrytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); + tx_tryte = init_trytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); if (!tx_tryte) return false; @@ -528,7 +531,7 @@ static bool PowAVX(void *pow_ctx) pitem[i].nonce = nonce_array[i]; pitem[i].n = i; pitem[i].lock = &ctx->lock; - pitem[i].stopPoW = &ctx->stopPoW; + pitem[i].stop_pow = &ctx->stop_pow; pitem[i].ret = 0; work_req[i].data = &pitem[i]; uv_queue_work(loop_ptr, &work_req[i], work_cb, NULL); @@ -536,17 +539,17 @@ static bool PowAVX(void *pow_ctx) uv_run(loop_ptr, UV_RUN_DEFAULT); - int completedIndex = -1; + int completed_index = -1; for (int i = 0; i < ctx->num_threads; i++) { if (pitem[i].n == -1) - completedIndex = i; + completed_index = i; ctx->pow_info.hash_count += (uint64_t)(pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1); } clock_gettime(CLOCK_REALTIME, &end_time); ctx->pow_info.time = diff_in_second(start_time, end_time); - nonce_trit = initTrits(nonce_array[completedIndex], NONCE_TRITS_LENGTH); + nonce_trit = init_trits(nonce_array[completed_index], NONCE_TRITS_LENGTH); if (!nonce_trit) { res = false; goto fail; @@ -564,22 +567,22 @@ static bool PowAVX(void *pow_ctx) /* Free resource */ uv_rwlock_destroy(&ctx->lock); free(c_state); - freeTrobject(tx_tryte); - freeTrobject(nonce_trit); - freeTrobject(nonce_tryte); + free_trinary_object(tx_tryte); + free_trinary_object(nonce_trit); + free_trinary_object(nonce_tryte); return res; } -static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx) +static bool pow_avx_context_initialize(impl_context_t *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_phys_proc(); int nproc = get_avail_phys_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false; - PoW_AVX_Context *ctx = (PoW_AVX_Context *) malloc(sizeof(PoW_AVX_Context) * - impl_ctx->num_max_thread); + pow_avx_context_t *ctx = (pow_avx_context_t *) malloc( + sizeof(pow_avx_context_t) * impl_ctx->num_max_thread); if (!ctx) return false; @@ -587,7 +590,7 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx) void *work_req_chunk = malloc(impl_ctx->num_max_thread * sizeof(uv_work_t) * nproc); void *pitem_chunk = - malloc(impl_ctx->num_max_thread * sizeof(Pwork_struct) * nproc); + malloc(impl_ctx->num_max_thread * sizeof(pwork_t) * nproc); void *nonce_ptr_chunk = malloc(impl_ctx->num_max_thread * sizeof(int8_t *) * nproc); void *nonce_chunk = @@ -596,16 +599,13 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx) goto fail; for (int i = 0; i < impl_ctx->num_max_thread; i++) { - ctx[i].work_req = - (uv_work_t *) (work_req_chunk + i * sizeof(uv_work_t) * nproc); - ctx[i].pitem = - (Pwork_struct *) (pitem_chunk + i * sizeof(Pwork_struct) * nproc); - ctx[i].nonce_array = - (int8_t **) (nonce_ptr_chunk + i * sizeof(int8_t *) * nproc); + ctx[i].work_req = (uv_work_t *) (work_req_chunk) + i * nproc; + ctx[i].pitem = (pwork_t *) (pitem_chunk) + i * nproc; + ctx[i].nonce_array = (int8_t **) (nonce_ptr_chunk) + i * nproc; for (int j = 0; j < nproc; j++) - ctx[i].nonce_array[j] = - (int8_t *) (nonce_chunk + i * NONCE_TRITS_LENGTH * nproc + - j * NONCE_TRITS_LENGTH); + ctx[i].nonce_array[j] = (int8_t *) (nonce_chunk) + + i * NONCE_TRITS_LENGTH * nproc + + j * NONCE_TRITS_LENGTH; ctx[i].num_max_threads = nproc; impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1; uv_loop_init(&ctx[i].loop); @@ -627,9 +627,9 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx) return false; } -static void PoWAVX_Context_Destroy(ImplContext *impl_ctx) +static void pow_avx_context_destroy(impl_context_t *impl_ctx) { - PoW_AVX_Context *ctx = (PoW_AVX_Context *) impl_ctx->context; + pow_avx_context_t *ctx = (pow_avx_context_t *) impl_ctx->context; for (int i = 0; i < impl_ctx->num_max_thread; i++) { uv_loop_close(&ctx[i].loop); } @@ -640,21 +640,21 @@ static void PoWAVX_Context_Destroy(ImplContext *impl_ctx) free(ctx); } -static void *PoWAVX_getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads) +static void *pow_avx_get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { uv_mutex_lock(&impl_ctx->lock); for (int i = 0; i < impl_ctx->num_max_thread; i++) { if (impl_ctx->bitmap & (0x1 << i)) { impl_ctx->bitmap &= ~(0x1 << i); uv_mutex_unlock(&impl_ctx->lock); - PoW_AVX_Context *ctx = - impl_ctx->context + sizeof(PoW_AVX_Context) * i; + pow_avx_context_t *ctx = + (pow_avx_context_t *) impl_ctx->context + i; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = i; + ctx->index_of_context = i; if (threads > 0 && threads < ctx->num_max_threads) ctx->num_threads = threads; else @@ -666,41 +666,42 @@ static void *PoWAVX_getPoWContext(ImplContext *impl_ctx, return NULL; /* It should not happen */ } -static bool PoWAVX_freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +static bool pow_avx_free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { uv_mutex_lock(&impl_ctx->lock); - impl_ctx->bitmap |= 0x1 << ((PoW_AVX_Context *) pow_ctx)->indexOfContext; + impl_ctx->bitmap |= 0x1 + << ((pow_avx_context_t *) pow_ctx)->index_of_context; uv_mutex_unlock(&impl_ctx->lock); return true; } -static int8_t *PoWAVX_getPoWResult(void *pow_ctx) +static int8_t *pow_avx_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * (TRANSACTION_TRYTES_LENGTH)); if (!ret) return NULL; - memcpy(ret, ((PoW_AVX_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_avx_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info PoWAVX_getPoWInfo(void *pow_ctx) +static pow_info_t pow_avx_get_pow_info(void *pow_ctx) { - return ((PoW_AVX_Context *) pow_ctx)->pow_info; + return ((pow_avx_context_t *) pow_ctx)->pow_info; } -ImplContext PoWAVX_Context = { +impl_context_t pow_avx_context = { .context = NULL, .description = "CPU (Intel AVX)", .bitmap = 0, .num_max_thread = 0, .num_working_thread = 0, - .initialize = PoWAVX_Context_Initialize, - .destroy = PoWAVX_Context_Destroy, - .getPoWContext = PoWAVX_getPoWContext, - .freePoWContext = PoWAVX_freePoWContext, - .doThePoW = PowAVX, - .getPoWResult = PoWAVX_getPoWResult, - .getPoWInfo = PoWAVX_getPoWInfo, + .initialize = pow_avx_context_initialize, + .destroy = pow_avx_context_destroy, + .get_pow_context = pow_avx_get_pow_context, + .free_pow_context = pow_avx_free_pow_context, + .do_the_pow = pow_avx, + .get_pow_result = pow_avx_get_pow_result, + .get_pow_info = pow_avx_get_pow_info, }; diff --git a/src/pow_avx.h b/src/pow_avx.h index 7818abc..a654331 100644 --- a/src/pow_avx.h +++ b/src/pow_avx.h @@ -8,39 +8,39 @@ #include "trinary.h" #include "uv.h" -typedef struct _pwork_struct Pwork_struct; +typedef struct pwork_s pwork_t; -struct _pwork_struct { +struct pwork_s { int8_t *mid; int mwm; int8_t *nonce; int n; uv_rwlock_t *lock; - int *stopPoW; + int *stop_pow; int64_t ret; }; -typedef struct _pow_avx_context PoW_AVX_Context; +typedef struct pow_avx_context_s pow_avx_context_t; -struct _pow_avx_context { +struct pow_avx_context_s { /* Resource of computing */ uv_rwlock_t lock; /* Data type of libtuv */ uv_loop_t loop; uv_work_t *work_req; - Pwork_struct *pitem; + pwork_t *pitem; int8_t **nonce_array; - int stopPoW; + int stop_pow; int num_threads; int num_max_threads; /* Management of Multi-thread */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; + pow_info_t pow_info; }; #ifdef _MSC_VER @@ -49,7 +49,7 @@ struct _pow_avx_context { #include #endif -#define INCR_START HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27 +#define INCR_START (HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27) #ifdef __AVX2__ #define HBITS 0xFFFFFFFFFFFFFFFFuLL diff --git a/src/pow_c.c b/src/pow_c.c index 9c4c155..0e14575 100644 --- a/src/pow_c.c +++ b/src/pow_c.c @@ -15,7 +15,7 @@ #include "curl.h" #include "implcontext.h" -static void transform64(uint64_t *lmid, uint64_t *hmid) +static void transform_64(uint64_t *lmid, uint64_t *hmid) { uint64_t alpha, beta, delta; uint64_t *lfrom = lmid, *hfrom = hmid; @@ -105,19 +105,19 @@ static long long int loop_cpu(uint64_t *lmid, uint64_t *hmid, int m, int8_t *nonce, - int *stopPoW, + int *stop_pow, uv_rwlock_t *lock) { - int n = 0; long long int i = 0; uint64_t lcpy[STATE_TRITS_LENGTH * 2], hcpy[STATE_TRITS_LENGTH * 2]; uv_rwlock_rdlock(lock); - for (i = 0; !incr(lmid, hmid) && !*stopPoW; i++) { + for (i = 0; !incr(lmid, hmid) && !*stop_pow; i++) { uv_rwlock_rdunlock(lock); + int n; memcpy(lcpy, lmid, STATE_TRITS_LENGTH * sizeof(uint64_t)); memcpy(hcpy, hmid, STATE_TRITS_LENGTH * sizeof(uint64_t)); - transform64(lcpy, hcpy); + transform_64(lcpy, hcpy); if ((n = check(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, m)) >= 0) { seri(lmid, hmid, n, nonce); @@ -149,7 +149,7 @@ static void para(int8_t in[], uint64_t l[], uint64_t h[]) } } -static void incrN(int n, uint64_t *mid_low, uint64_t *mid_high) +static void incr_n(int n, uint64_t *mid_low, uint64_t *mid_high) { for (int j = 0; j < n; j++) { uint64_t carry = 1; @@ -166,7 +166,7 @@ static int64_t pwork(int8_t mid[], int mwm, int8_t nonce[], int n, - int *stopPoW, + int *stop_pow, uv_rwlock_t *lock) { uint64_t lmid[STATE_TRITS_LENGTH] = {0}, hmid[STATE_TRITS_LENGTH] = {0}; @@ -181,33 +181,34 @@ static int64_t pwork(int8_t mid[], hmid[offset + 2] = HIGH2; lmid[offset + 3] = LOW3; hmid[offset + 3] = HIGH3; - incrN(n, lmid, hmid); + incr_n(n, lmid, hmid); - return loop_cpu(lmid, hmid, mwm, nonce, stopPoW, lock); + return loop_cpu(lmid, hmid, mwm, nonce, stop_pow, lock); } static void work_cb(uv_work_t *req) { - Pwork_struct *pworkInfo = (Pwork_struct *) req->data; - pworkInfo->ret = pwork(pworkInfo->mid, pworkInfo->mwm, pworkInfo->nonce, - pworkInfo->n, pworkInfo->stopPoW, pworkInfo->lock); - - uv_rwlock_wrlock(pworkInfo->lock); - if (pworkInfo->ret >= 0) { - *pworkInfo->stopPoW = 1; + pwork_t *pwork_info = (pwork_t *) req->data; + pwork_info->ret = + pwork(pwork_info->mid, pwork_info->mwm, pwork_info->nonce, + pwork_info->n, pwork_info->stop_pow, pwork_info->lock); + + uv_rwlock_wrlock(pwork_info->lock); + if (pwork_info->ret >= 0) { + *pwork_info->stop_pow = 1; /* This means this thread got the result */ - pworkInfo->n = -1; + pwork_info->n = -1; } - uv_rwlock_wrunlock(pworkInfo->lock); + uv_rwlock_wrunlock(pwork_info->lock); } -static int8_t *tx_to_cstate(Trytes_t *tx) +static int8_t *tx_to_cstate(trytes_t *tx) { - Trytes_t *inn = NULL; - Trits_t *tr = NULL; + trytes_t *inn = NULL; + trits_t *tr = NULL; int8_t tyt[TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] = {0}; - Curl *c = initCurl(); + curl_t *c = init_curl(); int8_t *c_state = (int8_t *) malloc(STATE_TRITS_LENGTH); if (!c || !c_state) goto fail; @@ -215,11 +216,11 @@ static int8_t *tx_to_cstate(Trytes_t *tx) /* Copy tx->data[:TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] to tyt */ memcpy(tyt, tx->data, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); - inn = initTrytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); + inn = init_trytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); if (!inn) goto fail; - Absorb(c, inn); + absorb(c, inn); tr = trits_from_trytes(tx); if (!tr) @@ -235,19 +236,19 @@ static int8_t *tx_to_cstate(Trytes_t *tx) c->state->len - tr->len + (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH)); - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); return c_state; fail: - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); free(c_state); return NULL; } -static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) +static void nonce_to_result(trytes_t *tx, trytes_t *nonce, int8_t *ret) { int rst_len = tx->len - NONCE_TRYTES_LENGTH + nonce->len; @@ -256,26 +257,26 @@ static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) rst_len - (tx->len - NONCE_TRYTES_LENGTH)); } -bool PowC(void *pow_ctx) +bool pow_c(void *pow_ctx) { bool res = true; - Trits_t *nonce_trit = NULL; - Trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; + trits_t *nonce_trit = NULL; + trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; struct timespec start_time, end_time; /* Initialize the context */ - PoW_C_Context *ctx = (PoW_C_Context *) pow_ctx; - ctx->stopPoW = 0; + pow_c_context_t *ctx = (pow_c_context_t *) pow_ctx; + ctx->stop_pow = 0; ctx->pow_info.time = 0; ctx->pow_info.hash_count = 0; uv_rwlock_init(&ctx->lock); uv_loop_t *loop_ptr = &ctx->loop; uv_work_t *work_req = ctx->work_req; - Pwork_struct *pitem = ctx->pitem; + pwork_t *pitem = ctx->pitem; int8_t **nonce_array = ctx->nonce_array; /* Prepare the input trytes for algorithm */ - tx_tryte = initTrytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); + tx_tryte = init_trytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); if (!tx_tryte) return false; @@ -293,7 +294,7 @@ bool PowC(void *pow_ctx) pitem[i].nonce = nonce_array[i]; pitem[i].n = i; pitem[i].lock = &ctx->lock; - pitem[i].stopPoW = &ctx->stopPoW; + pitem[i].stop_pow = &ctx->stop_pow; pitem[i].ret = 0; work_req[i].data = &pitem[i]; uv_queue_work(loop_ptr, &work_req[i], work_cb, NULL); @@ -301,17 +302,17 @@ bool PowC(void *pow_ctx) uv_run(loop_ptr, UV_RUN_DEFAULT); - int completedIndex = -1; + int completed_index = -1; for (int i = 0; i < ctx->num_threads; i++) { if (pitem[i].n == -1) - completedIndex = i; + completed_index = i; ctx->pow_info.hash_count += (uint64_t)(pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1); } clock_gettime(CLOCK_REALTIME, &end_time); ctx->pow_info.time = diff_in_second(start_time, end_time); - nonce_trit = initTrits(nonce_array[completedIndex], NONCE_TRITS_LENGTH); + nonce_trit = init_trits(nonce_array[completed_index], NONCE_TRITS_LENGTH); if (!nonce_trit) { res = false; goto fail; @@ -329,21 +330,21 @@ bool PowC(void *pow_ctx) /* Free resource */ uv_rwlock_destroy(&ctx->lock); free(c_state); - freeTrobject(tx_tryte); - freeTrobject(nonce_trit); - freeTrobject(nonce_tryte); + free_trinary_object(tx_tryte); + free_trinary_object(nonce_trit); + free_trinary_object(nonce_tryte); return res; } -static bool PoWC_Context_Initialize(ImplContext *impl_ctx) +static bool pow_c_context_initialize(impl_context_t *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_phys_proc(); int nproc = get_avail_phys_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false; - PoW_C_Context *ctx = (PoW_C_Context *) malloc(sizeof(PoW_C_Context) * - impl_ctx->num_max_thread); + pow_c_context_t *ctx = (pow_c_context_t *) malloc(sizeof(pow_c_context_t) * + impl_ctx->num_max_thread); if (!ctx) return false; @@ -351,7 +352,7 @@ static bool PoWC_Context_Initialize(ImplContext *impl_ctx) void *work_req_chunk = malloc(impl_ctx->num_max_thread * sizeof(uv_work_t) * nproc); void *pitem_chunk = - malloc(impl_ctx->num_max_thread * sizeof(Pwork_struct) * nproc); + malloc(impl_ctx->num_max_thread * sizeof(pwork_t) * nproc); void *nonce_ptr_chunk = malloc(impl_ctx->num_max_thread * sizeof(int8_t *) * nproc); void *nonce_chunk = @@ -360,16 +361,13 @@ static bool PoWC_Context_Initialize(ImplContext *impl_ctx) goto fail; for (int i = 0; i < impl_ctx->num_max_thread; i++) { - ctx[i].work_req = - (uv_work_t *) (work_req_chunk + i * sizeof(uv_work_t) * nproc); - ctx[i].pitem = - (Pwork_struct *) (pitem_chunk + i * sizeof(Pwork_struct) * nproc); - ctx[i].nonce_array = - (int8_t **) (nonce_ptr_chunk + i * sizeof(int8_t *) * nproc); + ctx[i].work_req = (uv_work_t *) (work_req_chunk) + i * nproc; + ctx[i].pitem = (pwork_t *) (pitem_chunk) + i * nproc; + ctx[i].nonce_array = (int8_t **) (nonce_ptr_chunk) + i * nproc; for (int j = 0; j < nproc; j++) - ctx[i].nonce_array[j] = - (int8_t *) (nonce_chunk + i * NONCE_TRITS_LENGTH * nproc + - j * NONCE_TRITS_LENGTH); + ctx[i].nonce_array[j] = (int8_t *) (nonce_chunk) + + i * NONCE_TRITS_LENGTH * nproc + + j * NONCE_TRITS_LENGTH; ctx[i].num_max_threads = nproc; impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1; uv_loop_init(&ctx[i].loop); @@ -391,9 +389,9 @@ static bool PoWC_Context_Initialize(ImplContext *impl_ctx) return false; } -static void PoWC_Context_Destroy(ImplContext *impl_ctx) +static void pow_c_context_destroy(impl_context_t *impl_ctx) { - PoW_C_Context *ctx = (PoW_C_Context *) impl_ctx->context; + pow_c_context_t *ctx = (pow_c_context_t *) impl_ctx->context; for (int i = 0; i < impl_ctx->num_max_thread; i++) { uv_loop_close(&ctx[i].loop); } @@ -404,20 +402,20 @@ static void PoWC_Context_Destroy(ImplContext *impl_ctx) free(ctx); } -static void *PoWC_getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads) +static void *pow_c_get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { uv_mutex_lock(&impl_ctx->lock); for (int i = 0; i < impl_ctx->num_max_thread; i++) { if (impl_ctx->bitmap & (0x1 << i)) { impl_ctx->bitmap &= ~(0x1 << i); uv_mutex_unlock(&impl_ctx->lock); - PoW_C_Context *ctx = impl_ctx->context + sizeof(PoW_C_Context) * i; + pow_c_context_t *ctx = (pow_c_context_t *) impl_ctx->context + i; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = i; + ctx->index_of_context = i; if (threads > 0 && threads < ctx->num_max_threads) ctx->num_threads = threads; else @@ -429,41 +427,41 @@ static void *PoWC_getPoWContext(ImplContext *impl_ctx, return NULL; /* It should not happen */ } -static bool PoWC_freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +static bool pow_c_free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { uv_mutex_lock(&impl_ctx->lock); - impl_ctx->bitmap |= 0x1 << ((PoW_C_Context *) pow_ctx)->indexOfContext; + impl_ctx->bitmap |= 0x1 << ((pow_c_context_t *) pow_ctx)->index_of_context; uv_mutex_unlock(&impl_ctx->lock); return true; } -static int8_t *PoWC_getPoWResult(void *pow_ctx) +static int8_t *pow_c_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * (TRANSACTION_TRYTES_LENGTH)); if (!ret) return NULL; - memcpy(ret, ((PoW_C_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_c_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info PoWC_getPoWInfo(void *pow_ctx) +static pow_info_t pow_c_get_pow_info(void *pow_ctx) { - return ((PoW_C_Context *) pow_ctx)->pow_info; + return ((pow_c_context_t *) pow_ctx)->pow_info; } -ImplContext PoWC_Context = { +impl_context_t pow_c_context = { .context = NULL, .description = "CPU (Pure C)", .bitmap = 0, .num_max_thread = 0, .num_working_thread = 0, - .initialize = PoWC_Context_Initialize, - .destroy = PoWC_Context_Destroy, - .getPoWContext = PoWC_getPoWContext, - .freePoWContext = PoWC_freePoWContext, - .doThePoW = PowC, - .getPoWResult = PoWC_getPoWResult, - .getPoWInfo = PoWC_getPoWInfo, + .initialize = pow_c_context_initialize, + .destroy = pow_c_context_destroy, + .get_pow_context = pow_c_get_pow_context, + .free_pow_context = pow_c_free_pow_context, + .do_the_pow = pow_c, + .get_pow_result = pow_c_get_pow_result, + .get_pow_info = pow_c_get_pow_info, }; diff --git a/src/pow_c.h b/src/pow_c.h index bab7478..0851dfb 100644 --- a/src/pow_c.h +++ b/src/pow_c.h @@ -16,45 +16,45 @@ #include "trinary.h" #include "uv.h" -typedef struct _pwork_struct Pwork_struct; +typedef struct pwork_s pwork_t; -struct _pwork_struct { +struct pwork_s { int8_t *mid; int mwm; int8_t *nonce; int n; uv_rwlock_t *lock; - int *stopPoW; + int *stop_pow; int index; int64_t ret; }; -typedef struct _pow_c_context PoW_C_Context; +typedef struct pow_c_context_s pow_c_context_t; -struct _pow_c_context { +struct pow_c_context_s { /* Resource of computing */ uv_rwlock_t lock; /* Data type of libtuv */ uv_loop_t loop; uv_work_t *work_req; - Pwork_struct *pitem; + pwork_t *pitem; int8_t **nonce_array; - int stopPoW; + int stop_pow; int num_threads; int num_max_threads; /* Management of Multi-thread */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; + pow_info_t pow_info; }; #define HBITS 0xFFFFFFFFFFFFFFFFuLL #define LBITS 0x0000000000000000uLL -#define INCR_START HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27 +#define INCR_START (HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27) #define LOW0 \ 0xDB6DB6DB6DB6DB6DuLL // 0b1101101101101101101101101101101101101101101101101101101101101101L; #define HIGH0 \ diff --git a/src/pow_cl.c b/src/pow_cl.c index 2b3ef15..450b61a 100644 --- a/src/pow_cl.c +++ b/src/pow_cl.c @@ -22,31 +22,31 @@ #include "curl.h" #include "implcontext.h" -static CLContext _opencl_ctx[MAX_NUM_DEVICES]; +static cl_context_t opencl_ctx[MAX_NUM_DEVICES]; -static bool write_cl_buffer(CLContext *ctx, +static bool write_cl_buffer(cl_context_t *ctx, int64_t *mid_low, int64_t *mid_high, int mwm, int loop_count) { - cl_command_queue cmdq = ctx->cmdq; - cl_mem *memobj = ctx->buffer; - BufferInfo *buffer_info = ctx->kernel_info.buffer_info; + cl_command_queue cmd_q = ctx->cmd_q; + cl_mem *mem_obj = ctx->buffer; + buffer_info_t *buffer_info = ctx->kernel_info.buffer_info; - if (clEnqueueWriteBuffer(cmdq, memobj[INDEX_OF_MID_LOW], CL_TRUE, 0, + if (clEnqueueWriteBuffer(cmd_q, mem_obj[INDEX_OF_MID_LOW], CL_TRUE, 0, buffer_info[INDEX_OF_MID_LOW].size, mid_low, 0, NULL, NULL) != CL_SUCCESS) return false; - if (clEnqueueWriteBuffer(cmdq, memobj[INDEX_OF_MID_HIGH], CL_TRUE, 0, + if (clEnqueueWriteBuffer(cmd_q, mem_obj[INDEX_OF_MID_HIGH], CL_TRUE, 0, buffer_info[INDEX_OF_MID_HIGH].size, mid_high, 0, NULL, NULL) != CL_SUCCESS) return false; - if (clEnqueueWriteBuffer(cmdq, memobj[INDEX_OF_MWM], CL_TRUE, 0, + if (clEnqueueWriteBuffer(cmd_q, mem_obj[INDEX_OF_MWM], CL_TRUE, 0, buffer_info[INDEX_OF_MWM].size, &mwm, 0, NULL, NULL) != CL_SUCCESS) return false; - if (clEnqueueWriteBuffer(cmdq, memobj[INDEX_OF_LOOP_COUNT], CL_TRUE, 0, + if (clEnqueueWriteBuffer(cmd_q, mem_obj[INDEX_OF_LOOP_COUNT], CL_TRUE, 0, buffer_info[INDEX_OF_LOOP_COUNT].size, &loop_count, 0, NULL, NULL) != CL_SUCCESS) return false; @@ -83,12 +83,12 @@ static void init_state(int8_t *state, mid_high[offset + 3] = HIGH_3; } -static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) +static int8_t *pwork(int8_t *state, int mwm, cl_context_t *ctx) { size_t local_work_size, global_work_size, global_offset, num_groups; char found = 0; cl_event ev, ev1; - CLContext *titan = ctx; + cl_context_t *titan = ctx; ctx->hash_count = 0; global_offset = 0; num_groups = titan->num_cores; @@ -107,10 +107,10 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) if (!write_cl_buffer(titan, mid_low, mid_high, mwm, LOOP_COUNT)) return NULL; - if (CL_SUCCESS == - clEnqueueNDRangeKernel(titan->cmdq, titan->kernel[INDEX_OF_KERNEL_INIT], - 1, &global_offset, &global_work_size, - &local_work_size, 0, NULL, &ev)) { + if (CL_SUCCESS == clEnqueueNDRangeKernel( + titan->cmd_q, titan->kernel[INDEX_OF_KERNEL_INIT], 1, + &global_offset, &global_work_size, &local_work_size, + 0, NULL, &ev)) { clWaitForEvents(1, &ev); clReleaseEvent(ev); ctx->hash_count += 64 * num_groups * LOOP_COUNT; @@ -118,14 +118,14 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) while (found == 0) { if (CL_SUCCESS != clEnqueueNDRangeKernel( - titan->cmdq, titan->kernel[INDEX_OF_KERNEL_SEARCH], 1, NULL, - &global_work_size, &local_work_size, 0, NULL, &ev1)) { + titan->cmd_q, titan->kernel[INDEX_OF_KERNEL_SEARCH], 1, + NULL, &global_work_size, &local_work_size, 0, NULL, &ev1)) { clReleaseEvent(ev1); return NULL; /* Running "search" kernel function failed */ } clWaitForEvents(1, &ev1); clReleaseEvent(ev1); - if (CL_SUCCESS != clEnqueueReadBuffer(titan->cmdq, + if (CL_SUCCESS != clEnqueueReadBuffer(titan->cmd_q, titan->buffer[INDEX_OF_FOUND], CL_TRUE, 0, sizeof(char), &found, 0, NULL, NULL)) { @@ -137,7 +137,7 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) } if (CL_SUCCESS != clEnqueueNDRangeKernel( - titan->cmdq, titan->kernel[INDEX_OF_KERNEL_FINALIZE], + titan->cmd_q, titan->kernel[INDEX_OF_KERNEL_FINALIZE], 1, NULL, &global_work_size, &local_work_size, 0, NULL, &ev)) { return NULL; /* Running "finalize" kernel function failed */ @@ -149,7 +149,7 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) if (found > 0) { if (CL_SUCCESS != - clEnqueueReadBuffer(titan->cmdq, titan->buffer[INDEX_OF_TRIT_HASH], + clEnqueueReadBuffer(titan->cmd_q, titan->buffer[INDEX_OF_TRIT_HASH], CL_TRUE, 0, HASH_TRITS_LENGTH * sizeof(int8_t), buf, 1, &ev, NULL)) { return NULL; /* Read buffer failed */ @@ -159,13 +159,13 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx) return buf; } -static int8_t *tx_to_cstate(Trytes_t *tx) +static int8_t *tx_to_cstate(trytes_t *tx) { - Trytes_t *inn = NULL; - Trits_t *tr = NULL; + trytes_t *inn = NULL; + trits_t *tr = NULL; int8_t tyt[TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] = {0}; - Curl *c = initCurl(); + curl_t *c = init_curl(); int8_t *c_state = (int8_t *) malloc(STATE_TRITS_LENGTH); if (!c || !c_state) goto fail; @@ -173,11 +173,11 @@ static int8_t *tx_to_cstate(Trytes_t *tx) /* Copy tx->data[:TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] to tyt */ memcpy(tyt, tx->data, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); - inn = initTrytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); + inn = init_trytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); if (!inn) goto fail; - Absorb(c, inn); + absorb(c, inn); tr = trits_from_trytes(tx); if (!tr) @@ -193,31 +193,33 @@ static int8_t *tx_to_cstate(Trytes_t *tx) c->state->len - tr->len + (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH)); - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); return c_state; fail: - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); free(c_state); return NULL; } -static bool PowCL(void *pow_ctx) +static bool pow_cl(void *pow_ctx) { bool res = true; int8_t *c_state = NULL, *pow_result = NULL; - Trits_t *tx_trit = NULL; - Trytes_t *tx_tryte = NULL, *res_tryte = NULL; + trits_t *tx_trit = NULL; + trytes_t *tx_tryte, *res_tryte = NULL; struct timespec start_time, end_time; - PoW_CL_Context *ctx = (PoW_CL_Context *) pow_ctx; + pow_cl_context_t *ctx = (pow_cl_context_t *) pow_ctx; - tx_tryte = initTrytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); - if (!tx_tryte) - return false; + tx_tryte = init_trytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); + if (!tx_tryte) { + res = false; + goto fail; + } tx_trit = trits_from_trytes(tx_tryte); if (!tx_trit) { @@ -232,7 +234,7 @@ static bool PowCL(void *pow_ctx) } clock_gettime(CLOCK_REALTIME, &start_time); - pow_result = pwork(c_state, ctx->mwm, ctx->clctx); + pow_result = pwork(c_state, ctx->mwm, ctx->cl_ctx); clock_gettime(CLOCK_REALTIME, &end_time); ctx->pow_info.time = diff_in_second(start_time, end_time); if (!pow_result) { @@ -249,27 +251,27 @@ static bool PowCL(void *pow_ctx) } memcpy(ctx->output_trytes, res_tryte->data, TRANSACTION_TRYTES_LENGTH); - ctx->pow_info.hash_count = ctx->clctx->hash_count; + ctx->pow_info.hash_count = ctx->cl_ctx->hash_count; fail: - freeTrobject(tx_trit); - freeTrobject(tx_tryte); - freeTrobject(res_tryte); + free_trinary_object(tx_trit); + free_trinary_object(tx_tryte); + free_trinary_object(res_tryte); free(c_state); free(pow_result); return res; } -static bool PoWCL_Context_Initialize(ImplContext *impl_ctx) +static bool pow_cl_context_initialize(impl_context_t *impl_ctx) { - impl_ctx->num_max_thread = init_clcontext(_opencl_ctx); - PoW_CL_Context *ctx = (PoW_CL_Context *) malloc(sizeof(PoW_CL_Context) * - impl_ctx->num_max_thread); + impl_ctx->num_max_thread = init_clcontext(opencl_ctx); + pow_cl_context_t *ctx = (pow_cl_context_t *) malloc( + sizeof(pow_cl_context_t) * impl_ctx->num_max_thread); if (!ctx) goto fail; for (int i = 0; i < impl_ctx->num_max_thread; i++) { - ctx[i].clctx = &_opencl_ctx[i]; + ctx[i].cl_ctx = &opencl_ctx[i]; impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1; } impl_ctx->context = ctx; @@ -281,27 +283,26 @@ static bool PoWCL_Context_Initialize(ImplContext *impl_ctx) return false; } -static void PoWCL_Context_Destroy(ImplContext *impl_ctx) +static void pow_cl_context_destroy(impl_context_t *impl_ctx) { - PoW_CL_Context *ctx = (PoW_CL_Context *) impl_ctx->context; + pow_cl_context_t *ctx = (pow_cl_context_t *) impl_ctx->context; free(ctx); } -static void *PoWCL_getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads) +static void *pow_cl_get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { uv_mutex_lock(&impl_ctx->lock); for (int i = 0; i < impl_ctx->num_max_thread; i++) { if (impl_ctx->bitmap & (0x1 << i)) { impl_ctx->bitmap &= ~(0x1 << i); uv_mutex_unlock(&impl_ctx->lock); - PoW_CL_Context *ctx = - impl_ctx->context + sizeof(PoW_CL_Context) * i; + pow_cl_context_t *ctx = (pow_cl_context_t *) impl_ctx->context + i; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = i; + ctx->index_of_context = i; return ctx; } } @@ -309,41 +310,41 @@ static void *PoWCL_getPoWContext(ImplContext *impl_ctx, return NULL; /* It should not happen */ } -static bool PoWCL_freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +static bool pow_cl_free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { uv_mutex_lock(&impl_ctx->lock); - impl_ctx->bitmap |= 0x1 << ((PoW_CL_Context *) pow_ctx)->indexOfContext; + impl_ctx->bitmap |= 0x1 << ((pow_cl_context_t *) pow_ctx)->index_of_context; uv_mutex_unlock(&impl_ctx->lock); return true; } -static int8_t *PoWCL_getPoWResult(void *pow_ctx) +static int8_t *pow_cl_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * (TRANSACTION_TRYTES_LENGTH)); if (!ret) return NULL; - memcpy(ret, ((PoW_CL_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_cl_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info PoWCL_getPoWInfo(void *pow_ctx) +static pow_info_t pow_cl_get_pow_info(void *pow_ctx) { - return ((PoW_CL_Context *) pow_ctx)->pow_info; + return ((pow_cl_context_t *) pow_ctx)->pow_info; } -ImplContext PoWCL_Context = { +impl_context_t pow_cl_context = { .context = NULL, .description = "GPU (OpenCL)", .bitmap = 0, .num_max_thread = 0, .num_working_thread = 0, - .initialize = PoWCL_Context_Initialize, - .destroy = PoWCL_Context_Destroy, - .getPoWContext = PoWCL_getPoWContext, - .freePoWContext = PoWCL_freePoWContext, - .doThePoW = PowCL, - .getPoWResult = PoWCL_getPoWResult, - .getPoWInfo = PoWCL_getPoWInfo, + .initialize = pow_cl_context_initialize, + .destroy = pow_cl_context_destroy, + .get_pow_context = pow_cl_get_pow_context, + .free_pow_context = pow_cl_free_pow_context, + .do_the_pow = pow_cl, + .get_pow_result = pow_cl_get_pow_result, + .get_pow_info = pow_cl_get_pow_info, }; diff --git a/src/pow_cl.h b/src/pow_cl.h index 34c2cd7..5e00f7f 100644 --- a/src/pow_cl.h +++ b/src/pow_cl.h @@ -15,18 +15,18 @@ #include "constants.h" #include "trinary.h" -typedef struct _pow_cl_context PoW_CL_Context; +typedef struct pow_cl_context_s pow_cl_context_t; -struct _pow_cl_context { - CLContext *clctx; +struct pow_cl_context_s { + cl_context_t *cl_ctx; /* Management of Multi-thread */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; + pow_info_t pow_info; }; /* Number of GPU devices supported */ diff --git a/src/pow_fpga_accel.c b/src/pow_fpga.c similarity index 62% rename from src/pow_fpga_accel.c rename to src/pow_fpga.c index 1f20499..8e2b7f8 100644 --- a/src/pow_fpga_accel.c +++ b/src/pow_fpga.c @@ -7,7 +7,7 @@ * "LICENSE" at the root of this distribution. */ -#include "pow_fpga_accel.h" +#include "pow_fpga.h" #include #include #include @@ -31,7 +31,7 @@ /* Set FPGA operation frequency 100 MHz */ #define FPGA_OPERATION_FREQUENCY 100000000 -#define INT2STRING(I, S) \ +#define INT_TO_STRING(I, S) \ { \ S[0] = I & 0xff; \ S[1] = (I >> 8) & 0xff; \ @@ -39,11 +39,9 @@ S[3] = (I >> 24) & 0xff; \ } -static bool PoWFPGAAccel(void *pow_ctx) +static bool pow_fpga(void *pow_ctx) { - PoW_FPGA_Accel_Context *ctx = (PoW_FPGA_Accel_Context *) pow_ctx; - ctx->pow_info.time = 0; - ctx->pow_info.hash_count = 0; + pow_fpga_context_t *ctx = (pow_fpga_context_t *) pow_ctx; int8_t fpga_out_nonce_trit[NONCE_TRITS_LENGTH]; @@ -51,16 +49,18 @@ static bool PoWFPGAAccel(void *pow_ctx) char buf[4]; bool res = true; - uint32_t tick_cnt_l = 0; - uint32_t tick_cnt_h = 0; - uint64_t tick_cnt = 0; + uint32_t tick_cnt_l; + uint32_t tick_cnt_h; + uint64_t tick_cnt; - Trytes_t *object_tryte = NULL, *nonce_tryte = NULL; - Trits_t *object_trit = NULL, *object_nonce_trit = NULL; + trytes_t *object_tryte, *nonce_tryte = NULL; + trits_t *object_trit = NULL, *object_nonce_trit = NULL; - object_tryte = initTrytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); - if (!object_tryte) - return false; + object_tryte = init_trytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); + if (!object_tryte) { + res = false; + goto fail; + } object_trit = trits_from_trytes(object_tryte); if (!object_trit) { @@ -78,7 +78,7 @@ static bool PoWFPGAAccel(void *pow_ctx) goto fail; } - INT2STRING(ctx->mwm, buf); + INT_TO_STRING(ctx->mwm, buf); if (write(ctx->ctrl_fd, buf, sizeof(buf)) < 0) { res = false; goto fail; @@ -94,7 +94,7 @@ static bool PoWFPGAAccel(void *pow_ctx) goto fail; } - object_nonce_trit = initTrits(fpga_out_nonce_trit, NONCE_TRITS_LENGTH); + object_nonce_trit = init_trits(fpga_out_nonce_trit, NONCE_TRITS_LENGTH); if (!object_nonce_trit) { res = false; goto fail; @@ -113,22 +113,22 @@ static bool PoWFPGAAccel(void *pow_ctx) ctx->pow_info.time = (double) tick_cnt / (double) FPGA_OPERATION_FREQUENCY; ctx->pow_info.hash_count = *(ctx->cpow_map + HASH_CNT_REG_OFFSET); - memcpy(ctx->output_trytes, ctx->input_trytes, (NonceTrinaryOffset) / 3); - memcpy(ctx->output_trytes + ((NonceTrinaryOffset) / 3), nonce_tryte->data, - ((TRANSACTION_TRITS_LENGTH) - (NonceTrinaryOffset)) / 3); + memcpy(ctx->output_trytes, ctx->input_trytes, (NONCE_TRINARY_OFFSET) / 3); + memcpy(ctx->output_trytes + ((NONCE_TRINARY_OFFSET) / 3), nonce_tryte->data, + ((TRANSACTION_TRITS_LENGTH) - (NONCE_TRINARY_OFFSET)) / 3); fail: - freeTrobject(object_tryte); - freeTrobject(object_trit); - freeTrobject(object_nonce_trit); - freeTrobject(nonce_tryte); + free_trinary_object(object_tryte); + free_trinary_object(object_trit); + free_trinary_object(object_nonce_trit); + free_trinary_object(nonce_tryte); return res; } -static bool PoWFPGAAccel_Context_Initialize(ImplContext *impl_ctx) +static bool pow_fpga_context_initialize(impl_context_t *impl_ctx) { - PoW_FPGA_Accel_Context *ctx = - (PoW_FPGA_Accel_Context *) malloc(sizeof(PoW_FPGA_Accel_Context)); + pow_fpga_context_t *ctx = + (pow_fpga_context_t *) malloc(sizeof(pow_fpga_context_t)); if (!ctx) goto fail_to_malloc; @@ -148,28 +148,28 @@ static bool PoWFPGAAccel_Context_Initialize(ImplContext *impl_ctx) goto fail_to_open_odata; } - ctx->devmem_fd = open(DEV_MEM_FPGA, O_RDWR | O_SYNC); - if (ctx->devmem_fd < 0) { + ctx->dev_mem_fd = open(DEV_MEM_FPGA, O_RDWR | O_SYNC); + if (ctx->dev_mem_fd < 0) { perror("devmem open fail"); goto fail_to_open_mem; } ctx->fpga_regs_map = (uint32_t *) mmap(NULL, HPS_TO_FPGA_SPAN, PROT_READ | PROT_WRITE, - MAP_SHARED, ctx->devmem_fd, HPS_TO_FPGA_BASE); + MAP_SHARED, ctx->dev_mem_fd, HPS_TO_FPGA_BASE); if (ctx->fpga_regs_map == MAP_FAILED) { perror("devmem mmap fial"); goto fail_to_mmap; } - ctx->cpow_map = (uint32_t *) (ctx->fpga_regs_map + CPOW_BASE); + ctx->cpow_map = (uint32_t *) (ctx->fpga_regs_map) + CPOW_BASE; impl_ctx->context = ctx; return true; fail_to_mmap: - close(ctx->devmem_fd); + close(ctx->dev_mem_fd); fail_to_open_mem: close(ctx->out_fd); fail_to_open_odata: @@ -177,13 +177,14 @@ static bool PoWFPGAAccel_Context_Initialize(ImplContext *impl_ctx) fail_to_open_idata: close(ctx->ctrl_fd); fail_to_open_ctrl: + free(ctx); fail_to_malloc: return false; } -static void PoWFPGAAccel_Context_Destroy(ImplContext *impl_ctx) +static void pow_fpga_context_destroy(impl_context_t *impl_ctx) { - PoW_FPGA_Accel_Context *ctx = (PoW_FPGA_Accel_Context *) impl_ctx->context; + pow_fpga_context_t *ctx = (pow_fpga_context_t *) impl_ctx->context; close(ctx->in_fd); close(ctx->out_fd); @@ -193,55 +194,55 @@ static void PoWFPGAAccel_Context_Destroy(ImplContext *impl_ctx) if (result < 0) { perror("devmem munmap fail"); } - close(ctx->devmem_fd); + close(ctx->dev_mem_fd); free(ctx); } -static void *PoWFPGAAccel_getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads) +static void *pow_fpga_get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { - PoW_FPGA_Accel_Context *ctx = impl_ctx->context; + pow_fpga_context_t *ctx = impl_ctx->context; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = 0; + ctx->index_of_context = 0; return ctx; } -static bool PoWFPGAAccel_freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +static bool pow_fpga_free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { return true; } -static int8_t *PoWFPGAAccel_getPoWResult(void *pow_ctx) +static int8_t *pow_fpga_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * TRANSACTION_TRYTES_LENGTH); if (!ret) return NULL; - memcpy(ret, ((PoW_FPGA_Accel_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_fpga_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info PoWFPGAAccel_getPoWInfo(void *pow_ctx) +static pow_info_t pow_fpga_get_pow_info(void *pow_ctx) { - return ((PoW_FPGA_Accel_Context *) pow_ctx)->pow_info; + return ((pow_fpga_context_t *) pow_ctx)->pow_info; } -ImplContext PoWFPGAAccel_Context = { +impl_context_t pow_fpga_context = { .context = NULL, .description = "FPGA", .bitmap = 0, .num_max_thread = 1, // num_max_thread >= 1 .num_working_thread = 0, - .initialize = PoWFPGAAccel_Context_Initialize, - .destroy = PoWFPGAAccel_Context_Destroy, - .getPoWContext = PoWFPGAAccel_getPoWContext, - .freePoWContext = PoWFPGAAccel_freePoWContext, - .doThePoW = PoWFPGAAccel, - .getPoWResult = PoWFPGAAccel_getPoWResult, - .getPoWInfo = PoWFPGAAccel_getPoWInfo, + .initialize = pow_fpga_context_initialize, + .destroy = pow_fpga_context_destroy, + .get_pow_context = pow_fpga_get_pow_context, + .free_pow_context = pow_fpga_free_pow_context, + .do_the_pow = pow_fpga, + .get_pow_result = pow_fpga_get_pow_result, + .get_pow_info = pow_fpga_get_pow_info, }; diff --git a/src/pow_fpga_accel.h b/src/pow_fpga.h similarity index 73% rename from src/pow_fpga_accel.h rename to src/pow_fpga.h index 119018c..19e3e60 100644 --- a/src/pow_fpga_accel.h +++ b/src/pow_fpga.h @@ -6,29 +6,29 @@ * "LICENSE" at the root of this distribution. */ -#ifndef POW_FPGA_ACCEL_H_ -#define POW_FPGA_ACCEL_H_ +#ifndef POW_FPGA_H_ +#define POW_FPGA_H_ #include #include "common.h" #include "constants.h" -typedef struct _pow_fpga_accel_context PoW_FPGA_Accel_Context; +typedef struct pow_fpga_context_s pow_fpga_context_t; -struct _pow_fpga_accel_context { +struct pow_fpga_context_s { /* Management of Multi-thread */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; - /* Device files for the PFGA accelerator*/ + pow_info_t pow_info; + /* Device files for the FPGA accelerator*/ int ctrl_fd; int in_fd; int out_fd; - int devmem_fd; + int dev_mem_fd; /* Memory map of fpga */ void *fpga_regs_map; uint32_t *cpow_map; diff --git a/src/pow_kernel.cl b/src/pow_kernel.cl index 55295b1..09f82c1 100644 --- a/src/pow_kernel.cl +++ b/src/pow_kernel.cl @@ -19,13 +19,13 @@ #define HIGH_BITS 0xFFFFFFFFFFFFFFFF #define LOW_BITS 0x0000000000000000 //#define HIGH_BITS -//0b1111111111111111111111111111111111111111111111111111111111111111L +// 0b1111111111111111111111111111111111111111111111111111111111111111L //#define LOW_BITS -//0b0000000000000000000000000000000000000000000000000000000000000000L +// 0b0000000000000000000000000000000000000000000000000000000000000000L /** * t1 = j == 0? 0:(((j - 1)%2)+1)*HALF_LENGTH - ((j-1)>>1); */ -__constant size_t INDEX[STATE_TRITS_LENGTH + 1] = { +__constant size_t index[STATE_TRITS_LENGTH + 1] = { 0, 364, 728, 363, 727, 362, 726, 361, 725, 360, 724, 359, 723, 358, 722, 357, 721, 356, 720, 355, 719, 354, 718, 353, 717, 352, 716, 351, 715, 350, 714, 349, 713, 348, 712, 347, 711, 346, 710, 345, 709, 344, 708, 343, 707, @@ -82,188 +82,231 @@ __constant size_t INDEX[STATE_TRITS_LENGTH + 1] = { typedef long bc_trit_t; -void increment(__global bc_trit_t* mid_low, __global bc_trit_t* mid_high, - __private size_t from_index, __private size_t to_index); -void copy_mid_to_state(__global bc_trit_t* mid_low, __global bc_trit_t* mid_high, - __global bc_trit_t* state_low, __global bc_trit_t* state_high, - __private size_t id, __private size_t l_size, +void increment(__global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __private size_t from_index, + __private size_t to_index); +void copy_mid_to_state(__global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __private size_t id, + __private size_t l_size, __private size_t l_trits); -void transform(__global bc_trit_t* state_low, __global bc_trit_t* state_high, - __private size_t id, __private size_t l_size, +void transform(__global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __private size_t id, + __private size_t l_size, __private size_t l_trits); -void check(__global bc_trit_t* state_low, __global bc_trit_t* state_high, - __global volatile char* found, - __constant size_t* min_weight_magnitude, - __global bc_trit_t* nonce_probe, __private size_t gr_id); -void setup_ids(__private size_t* id, __private size_t* gid, - __private size_t* gr_id, __private size_t* l_size, - __private size_t* n_trits); +void check(__global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __global volatile char *found, + __constant size_t *min_weight_magnitude, + __global bc_trit_t *nonce_probe, + __private size_t gr_id); +void setup_ids(__private size_t *id, + __private size_t *gid, + __private size_t *gr_id, + __private size_t *l_size, + __private size_t *n_trits); -void increment(__global bc_trit_t* mid_low, __global bc_trit_t* mid_high, - __private size_t from_index, __private size_t to_index) { - size_t i; - bc_trit_t carry = 1; - bc_trit_t low, hi; - for (i = from_index; i < to_index && carry != 0; i++) { - low = mid_low[i]; - hi = mid_high[i]; - mid_low[i] = hi ^ low; - mid_high[i] = low; - carry = hi & (~low); - } -} - -void copy_mid_to_state(__global bc_trit_t* mid_low, __global bc_trit_t* mid_high, - __global bc_trit_t* state_low, __global bc_trit_t* state_high, - __private size_t id, __private size_t l_size, - __private size_t n_trits) { - size_t i, j; - for (i = 0; i < n_trits; i++) { - j = id + i * l_size; - state_low[j] = mid_low[j]; - state_high[j] = mid_high[j]; - } +void increment(__global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __private size_t from_index, + __private size_t to_index) +{ + size_t i; + bc_trit_t carry = 1; + for (i = from_index; i < to_index && carry != 0; i++) { + bc_trit_t low, hi; + low = mid_low[i]; + hi = mid_high[i]; + mid_low[i] = hi ^ low; + mid_high[i] = low; + carry = hi & (~low); + } } -void transform(__global bc_trit_t* state_low, __global bc_trit_t* state_high, - __private size_t id, __private size_t l_size, - __private size_t n_trits) { - __private size_t round, i, j, k; - __private bc_trit_t alpha, beta, delta, sp_low[3], sp_high[3]; - for (round = 0; round < NUMBER_OF_ROUNDS; round++) { +void copy_mid_to_state(__global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __private size_t id, + __private size_t l_size, + __private size_t n_trits) +{ + size_t i; for (i = 0; i < n_trits; i++) { - j = id + i * l_size; - k = j+1; - alpha = state_low[INDEX[j]]; - beta = state_high[INDEX[j]]; - delta = alpha & (beta ^ state_low[INDEX[k]]); - sp_low[i] = ~delta; - sp_high[i] = (alpha ^ state_high[INDEX[k]]) | delta; + size_t j; + j = id + i * l_size; + state_low[j] = mid_low[j]; + state_high[j] = mid_high[j]; } - barrier(CLK_LOCAL_MEM_FENCE); - for (i = 0; i < n_trits; i++) { - j = id + i * l_size; - state_low[j] = sp_low[i]; - state_high[j] = sp_high[i]; +} + +void transform(__global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __private size_t id, + __private size_t l_size, + __private size_t n_trits) +{ + __private size_t round, i, j, k; + __private bc_trit_t alpha, beta, delta, sp_low[3], sp_high[3]; + for (round = 0; round < NUMBER_OF_ROUNDS; round++) { + for (i = 0; i < n_trits; i++) { + j = id + i * l_size; + k = j + 1; + alpha = state_low[index[j]]; + beta = state_high[index[j]]; + delta = alpha & (beta ^ state_low[index[k]]); + sp_low[i] = ~delta; + sp_high[i] = (alpha ^ state_high[index[k]]) | delta; + } + barrier(CLK_LOCAL_MEM_FENCE); + for (i = 0; i < n_trits; i++) { + j = id + i * l_size; + state_low[j] = sp_low[i]; + state_high[j] = sp_high[i]; + } + barrier(CLK_LOCAL_MEM_FENCE); } - barrier(CLK_LOCAL_MEM_FENCE); - } } -void check(__global bc_trit_t* state_low, __global bc_trit_t* state_high, - __global volatile char* found, - __constant size_t* min_weight_magnitude, - __global bc_trit_t* nonce_probe, __private size_t gr_id) { - int i; - *nonce_probe = HIGH_BITS; - for (i = HASH_TRITS_LENGTH - *min_weight_magnitude; i < HASH_TRITS_LENGTH; i++) { - *nonce_probe &= ~(state_low[i] ^ state_high[i]); - if (*nonce_probe == 0) - return; - } - if (*nonce_probe != 0) { - //*nonce_probe = 1 << __builtin_ctzl(*nonce_probe); - *found = gr_id + 1; - } +void check(__global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __global volatile char *found, + __constant size_t *min_weight_magnitude, + __global bc_trit_t *nonce_probe, + __private size_t gr_id) +{ + int i; + *nonce_probe = HIGH_BITS; + for (i = HASH_TRITS_LENGTH - *min_weight_magnitude; i < HASH_TRITS_LENGTH; + i++) { + *nonce_probe &= ~(state_low[i] ^ state_high[i]); + if (*nonce_probe == 0) + return; + } + if (*nonce_probe != 0) { + //*nonce_probe = 1 << __builtin_ctzl(*nonce_probe); + *found = gr_id + 1; + } } -void setup_ids(__private size_t* id, __private size_t* gid, - __private size_t* gr_id, __private size_t* l_size, - __private size_t* n_trits) { - __private size_t l_rem; - *id = get_local_id(0); - *l_size = get_local_size(0); - *gr_id = get_global_id(0) / *l_size; - *gid = *gr_id * STATE_TRITS_LENGTH; - l_rem = STATE_TRITS_LENGTH % *l_size; - *n_trits = STATE_TRITS_LENGTH / *l_size; - *n_trits += l_rem == 0 ? 0 : 1; - *n_trits -= (*n_trits) * (*id) < STATE_TRITS_LENGTH ? 0 : 1; +void setup_ids(__private size_t *id, + __private size_t *gid, + __private size_t *gr_id, + __private size_t *l_size, + __private size_t *n_trits) +{ + __private size_t l_rem; + *id = get_local_id(0); + *l_size = get_local_size(0); + *gr_id = get_global_id(0) / *l_size; + *gid = *gr_id * STATE_TRITS_LENGTH; + l_rem = STATE_TRITS_LENGTH % *l_size; + *n_trits = STATE_TRITS_LENGTH / *l_size; + *n_trits += l_rem == 0 ? 0 : 1; + *n_trits -= (*n_trits) * (*id) < STATE_TRITS_LENGTH ? 0 : 1; } -__kernel void init(__global char* trit_hash, __global bc_trit_t* mid_low, - __global bc_trit_t* mid_high, __global bc_trit_t* state_low, - __global bc_trit_t* state_high, - __constant size_t* min_weight_magnitude, - __global volatile char* found, __global bc_trit_t* nonce_probe, - __constant size_t* loop_count) { - __private size_t i, j, id, gid, gr_id, gl_off, l_size, n_trits; - setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); - gl_off = get_global_offset(0); +// cppcheck-suppress unusedFunction ; The invoked functions of OpenCL are not checked correctly with CppCheck +__kernel void init(__global char *trit_hash, + __global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __constant size_t *min_weight_magnitude, + __global volatile char *found, + __global bc_trit_t *nonce_probe, + __constant size_t *loop_count) +{ + __private size_t i, j, id, gid, gr_id, gl_off, l_size, n_trits; + setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); + gl_off = get_global_offset(0); - if (id == 0 && gr_id == 0) { - *found = 0; - } + if (id == 0 && gr_id == 0) { + *found = 0; + } - if (gr_id == 0) - return; + if (gr_id == 0) + return; - for (i = 0; i < n_trits; i++) { - j = id + i * l_size; - mid_low[gid + j] = mid_low[j]; - mid_high[gid + j] = mid_high[j]; - } + for (i = 0; i < n_trits; i++) { + j = id + i * l_size; + mid_low[gid + j] = mid_low[j]; + mid_high[gid + j] = mid_high[j]; + } - if (id == 0) { - for (i = 0; i < gr_id + gl_off; i++) { - increment(&(mid_low[gid]), &(mid_high[gid]), NONCE_INIT_START, - NONCE_INCREMENT_START); + if (id == 0) { + for (i = 0; i < gr_id + gl_off; i++) { + increment(&(mid_low[gid]), &(mid_high[gid]), NONCE_INIT_START, + NONCE_INCREMENT_START); + } } - } } -__kernel void search(__global char* trit_hash, __global bc_trit_t* mid_low, - __global bc_trit_t* mid_high, __global bc_trit_t* state_low, - __global bc_trit_t* state_high, - __constant size_t* min_weight_magnitude, - __global volatile char* found, - __global bc_trit_t* nonce_probe, - __constant size_t* loop_count) { - __private size_t i, id, gid, gr_id, l_size, n_trits; - setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); +// cppcheck-suppress unusedFunction ; The invoked functions of OpenCL are not checked correctly with CppCheck +__kernel void search(__global char *trit_hash, + __global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __constant size_t *min_weight_magnitude, + __global volatile char *found, + __global bc_trit_t *nonce_probe, + __constant size_t *loop_count) +{ + __private size_t i, id, gid, gr_id, l_size, n_trits; + setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); - for (i = 0; i < *loop_count; i++) { - if (id == 0) - increment(&(mid_low[gid]), &(mid_high[gid]), NONCE_INCREMENT_START, - HASH_TRITS_LENGTH); + for (i = 0; i < *loop_count; i++) { + if (id == 0) + increment(&(mid_low[gid]), &(mid_high[gid]), NONCE_INCREMENT_START, + HASH_TRITS_LENGTH); - barrier(CLK_LOCAL_MEM_FENCE); - copy_mid_to_state(&(mid_low[gid]), &(mid_high[gid]), &(state_low[gid]), - &(state_high[gid]), id, l_size, n_trits); + barrier(CLK_LOCAL_MEM_FENCE); + copy_mid_to_state(&(mid_low[gid]), &(mid_high[gid]), &(state_low[gid]), + &(state_high[gid]), id, l_size, n_trits); - barrier(CLK_LOCAL_MEM_FENCE); - transform(&(state_low[gid]), &(state_high[gid]), id, l_size, n_trits); + barrier(CLK_LOCAL_MEM_FENCE); + transform(&(state_low[gid]), &(state_high[gid]), id, l_size, n_trits); - barrier(CLK_LOCAL_MEM_FENCE); - if (id == 0) - check(&(state_low[gid]), &(state_high[gid]), found, min_weight_magnitude, - &(nonce_probe[gr_id]), gr_id); + barrier(CLK_LOCAL_MEM_FENCE); + if (id == 0) + check(&(state_low[gid]), &(state_high[gid]), found, + min_weight_magnitude, &(nonce_probe[gr_id]), gr_id); - barrier(CLK_LOCAL_MEM_FENCE); - if (*found != 0) - break; - } + barrier(CLK_LOCAL_MEM_FENCE); + if (*found != 0) + break; + } } -__kernel void finalize(__global char* trit_hash, __global bc_trit_t* mid_low, - __global bc_trit_t* mid_high, __global bc_trit_t* state_low, - __global bc_trit_t* state_high, - __constant size_t* min_weight_magnitude, - __global volatile char* found, - __global bc_trit_t* nonce_probe, - __constant size_t* loop_count) { - __private size_t i, j, id, gid, gr_id, l_size, n_trits; - setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); +// cppcheck-suppress unusedFunction ; The invoked functions of OpenCL are not checked correctly with CppCheck +__kernel void finalize(__global char *trit_hash, + __global bc_trit_t *mid_low, + __global bc_trit_t *mid_high, + __global bc_trit_t *state_low, + __global bc_trit_t *state_high, + __constant size_t *min_weight_magnitude, + __global volatile char *found, + __global bc_trit_t *nonce_probe, + __constant size_t *loop_count) +{ + __private size_t i, j, id, gid, gr_id, l_size, n_trits; + setup_ids(&id, &gid, &gr_id, &l_size, &n_trits); - if (gr_id == (size_t)(*found - 1) && nonce_probe[gr_id] != 0) { - for (i = 0; i < n_trits; i++) { - j = id + i * l_size; - if (j < HASH_TRITS_LENGTH) { - trit_hash[j] = - (mid_low[gid + j] & nonce_probe[gr_id]) == 0 - ? 1 - : (mid_high[gid + j] & nonce_probe[gr_id]) == 0 ? -1 : 0; - } + if (gr_id == (size_t)(*found - 1) && nonce_probe[gr_id] != 0) { + for (i = 0; i < n_trits; i++) { + j = id + i * l_size; + if (j < HASH_TRITS_LENGTH) { + trit_hash[j] = + (mid_low[gid + j] & nonce_probe[gr_id]) == 0 + ? 1 + : (mid_high[gid + j] & nonce_probe[gr_id]) == 0 ? -1 + : 0; + } + } } - } } diff --git a/src/pow_sse.c b/src/pow_sse.c index 8bb9567..f6bcd61 100644 --- a/src/pow_sse.c +++ b/src/pow_sse.c @@ -16,7 +16,7 @@ #include "curl.h" #include "implcontext.h" -static void transform128(__m128i *lmid, __m128i *hmid) +static void transform_128(__m128i *lmid, __m128i *hmid) { int t1, t2; __m128i alpha, beta, delta; @@ -50,7 +50,7 @@ static void transform128(__m128i *lmid, __m128i *hmid) } } -static int incr128(__m128i *mid_low, __m128i *mid_high) +static int incr_128(__m128i *mid_low, __m128i *mid_high) { int i; __m128i carry; @@ -66,7 +66,7 @@ static int incr128(__m128i *mid_low, __m128i *mid_high) return i == HASH_TRITS_LENGTH; } -static void seri128(__m128i *low, __m128i *high, int n, int8_t *r) +static void seri_128(__m128i *low, __m128i *high, int n, int8_t *r) { int index = 0; @@ -91,7 +91,7 @@ static void seri128(__m128i *low, __m128i *high, int n, int8_t *r) } } -static int check128(__m128i *l, __m128i *h, int m) +static int check_128(__m128i *l, __m128i *h, int m) { __m128i nonce_probe = _mm_set_epi64x(HBITS, HBITS); @@ -111,30 +111,30 @@ static int check128(__m128i *l, __m128i *h, int m) return -2; } -static int64_t loop128(__m128i *lmid, - __m128i *hmid, - int m, - int8_t *nonce, - int *stopPoW, - uv_rwlock_t *lock) +static int64_t loop_128(__m128i *lmid, + __m128i *hmid, + int m, + int8_t *nonce, + int *stop_pow, + uv_rwlock_t *lock) { - int n = 0; int64_t i = 0; __m128i lcpy[STATE_TRITS_LENGTH * 2], hcpy[STATE_TRITS_LENGTH * 2]; uv_rwlock_rdlock(lock); - for (i = 0; !incr128(lmid, hmid) && !*stopPoW; i++) { + for (i = 0; !incr_128(lmid, hmid) && !*stop_pow; i++) { uv_rwlock_rdunlock(lock); + int n; for (int j = 0; j < STATE_TRITS_LENGTH; j++) { lcpy[j] = lmid[j]; hcpy[j] = hmid[j]; } - transform128(lcpy, hcpy); + transform_128(lcpy, hcpy); - if ((n = check128(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, - m)) >= 0) { - seri128(lmid, hmid, n, nonce); + if ((n = check_128(lcpy + STATE_TRITS_LENGTH, hcpy + STATE_TRITS_LENGTH, + m)) >= 0) { + seri_128(lmid, hmid, n, nonce); return i * 128; } uv_rwlock_rdlock(lock); @@ -143,7 +143,7 @@ static int64_t loop128(__m128i *lmid, return -i * 128 - 1; } -static void para128(int8_t in[], __m128i l[], __m128i h[]) +static void para_128(int8_t in[], __m128i l[], __m128i h[]) { for (int i = 0; i < STATE_TRITS_LENGTH; i++) { switch (in[i]) { @@ -163,7 +163,7 @@ static void para128(int8_t in[], __m128i l[], __m128i h[]) } } -static void incrN128(int n, __m128i *mid_low, __m128i *mid_high) +static void incr_n_128(int n, __m128i *mid_low, __m128i *mid_high) { for (int j = 0; j < n; j++) { __m128i carry = _mm_set_epi64x(HBITS, HBITS); @@ -177,15 +177,15 @@ static void incrN128(int n, __m128i *mid_low, __m128i *mid_high) } } -static int64_t pwork128(int8_t mid[], - int mwm, - int8_t nonce[], - int n, - int *stopPoW, - uv_rwlock_t *lock) +static int64_t pwork_128(int8_t mid[], + int mwm, + int8_t nonce[], + int n, + int *stop_pow, + uv_rwlock_t *lock) { __m128i lmid[STATE_TRITS_LENGTH], hmid[STATE_TRITS_LENGTH]; - para128(mid, lmid, hmid); + para_128(mid, lmid, hmid); int offset = HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH; lmid[offset] = _mm_set_epi64x(LOW00, LOW01); @@ -198,34 +198,34 @@ static int64_t pwork128(int8_t mid[], hmid[offset + 3] = _mm_set_epi64x(HIGH30, HIGH31); lmid[offset + 4] = _mm_set_epi64x(LOW40, LOW41); hmid[offset + 4] = _mm_set_epi64x(HIGH40, HIGH41); - incrN128(n, lmid, hmid); + incr_n_128(n, lmid, hmid); - return loop128(lmid, hmid, mwm, nonce, stopPoW, lock); + return loop_128(lmid, hmid, mwm, nonce, stop_pow, lock); } static void work_cb(uv_work_t *req) { - Pwork_struct *pworkInfo = (Pwork_struct *) req->data; - pworkInfo->ret = - pwork128(pworkInfo->mid, pworkInfo->mwm, pworkInfo->nonce, pworkInfo->n, - pworkInfo->stopPoW, pworkInfo->lock); - - uv_rwlock_wrlock(pworkInfo->lock); - if (pworkInfo->ret >= 0) { - *pworkInfo->stopPoW = 1; + pwork_t *pwork_info = (pwork_t *) req->data; + pwork_info->ret = + pwork_128(pwork_info->mid, pwork_info->mwm, pwork_info->nonce, + pwork_info->n, pwork_info->stop_pow, pwork_info->lock); + + uv_rwlock_wrlock(pwork_info->lock); + if (pwork_info->ret >= 0) { + *pwork_info->stop_pow = 1; /* This means this thread got the result */ - pworkInfo->n = -1; + pwork_info->n = -1; } - uv_rwlock_wrunlock(pworkInfo->lock); + uv_rwlock_wrunlock(pwork_info->lock); } -static int8_t *tx_to_cstate(Trytes_t *tx) +static int8_t *tx_to_cstate(trytes_t *tx) { - Trytes_t *inn = NULL; - Trits_t *tr = NULL; + trytes_t *inn = NULL; + trits_t *tr = NULL; int8_t tyt[TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] = {0}; - Curl *c = initCurl(); + curl_t *c = init_curl(); int8_t *c_state = (int8_t *) malloc(STATE_TRITS_LENGTH); if (!c || !c_state) goto fail; @@ -233,11 +233,11 @@ static int8_t *tx_to_cstate(Trytes_t *tx) /* Copy tx->data[:TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] to tyt */ memcpy(tyt, tx->data, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); - inn = initTrytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); + inn = init_trytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH); if (!inn) goto fail; - Absorb(c, inn); + absorb(c, inn); tr = trits_from_trytes(tx); if (!tr) @@ -253,19 +253,19 @@ static int8_t *tx_to_cstate(Trytes_t *tx) c->state->len - tr->len + (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH)); - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); return c_state; fail: - freeTrobject(inn); - freeTrobject(tr); - freeCurl(c); + free_trinary_object(inn); + free_trinary_object(tr); + free_curl(c); free(c_state); return NULL; } -static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) +static void nonce_to_result(trytes_t *tx, trytes_t *nonce, int8_t *ret) { int rst_len = tx->len - NONCE_TRYTES_LENGTH + nonce->len; @@ -274,26 +274,26 @@ static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret) rst_len - (tx->len - NONCE_TRYTES_LENGTH)); } -static bool PowSSE(void *pow_ctx) +static bool pow_sse(void *pow_ctx) { bool res = true; - Trits_t *nonce_trit = NULL; - Trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; + trits_t *nonce_trit = NULL; + trytes_t *tx_tryte = NULL, *nonce_tryte = NULL; struct timespec start_time, end_time; /* Initialize the context */ - PoW_SSE_Context *ctx = (PoW_SSE_Context *) pow_ctx; - ctx->stopPoW = 0; + pow_sse_context_t *ctx = (pow_sse_context_t *) pow_ctx; + ctx->stop_pow = 0; ctx->pow_info.time = 0; ctx->pow_info.hash_count = 0; uv_rwlock_init(&ctx->lock); uv_loop_t *loop_ptr = &ctx->loop; uv_work_t *work_req = ctx->work_req; - Pwork_struct *pitem = ctx->pitem; + pwork_t *pitem = ctx->pitem; int8_t **nonce_array = ctx->nonce_array; /* Prepare the input trytes for algorithm */ - tx_tryte = initTrytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); + tx_tryte = init_trytes(ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); if (!tx_tryte) return false; @@ -311,7 +311,7 @@ static bool PowSSE(void *pow_ctx) pitem[i].nonce = nonce_array[i]; pitem[i].n = i; pitem[i].lock = &ctx->lock; - pitem[i].stopPoW = &ctx->stopPoW; + pitem[i].stop_pow = &ctx->stop_pow; pitem[i].ret = 0; work_req[i].data = &pitem[i]; uv_queue_work(loop_ptr, &work_req[i], work_cb, NULL); @@ -319,17 +319,17 @@ static bool PowSSE(void *pow_ctx) uv_run(loop_ptr, UV_RUN_DEFAULT); - int completedIndex = -1; + int completed_index = -1; for (int i = 0; i < ctx->num_threads; i++) { if (pitem[i].n == -1) - completedIndex = i; + completed_index = i; ctx->pow_info.hash_count += (uint64_t)(pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1); } clock_gettime(CLOCK_REALTIME, &end_time); ctx->pow_info.time = diff_in_second(start_time, end_time); - nonce_trit = initTrits(nonce_array[completedIndex], NONCE_TRITS_LENGTH); + nonce_trit = init_trits(nonce_array[completed_index], NONCE_TRITS_LENGTH); if (!nonce_trit) { res = false; goto fail; @@ -347,21 +347,21 @@ static bool PowSSE(void *pow_ctx) /* Free resource */ uv_rwlock_destroy(&ctx->lock); free(c_state); - freeTrobject(tx_tryte); - freeTrobject(nonce_trit); - freeTrobject(nonce_tryte); + free_trinary_object(tx_tryte); + free_trinary_object(nonce_trit); + free_trinary_object(nonce_tryte); return res; } -static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx) +static bool pow_sse_context_initialize(impl_context_t *impl_ctx) { impl_ctx->num_max_thread = get_nthds_per_phys_proc(); int nproc = get_avail_phys_nprocs(); if (impl_ctx->num_max_thread <= 0 || nproc <= 0) return false; - PoW_SSE_Context *ctx = (PoW_SSE_Context *) malloc(sizeof(PoW_SSE_Context) * - impl_ctx->num_max_thread); + pow_sse_context_t *ctx = (pow_sse_context_t *) malloc( + sizeof(pow_sse_context_t) * impl_ctx->num_max_thread); if (!ctx) return false; @@ -369,7 +369,7 @@ static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx) void *work_req_chunk = malloc(impl_ctx->num_max_thread * sizeof(uv_work_t) * nproc); void *pitem_chunk = - malloc(impl_ctx->num_max_thread * sizeof(Pwork_struct) * nproc); + malloc(impl_ctx->num_max_thread * sizeof(pwork_t) * nproc); void *nonce_ptr_chunk = malloc(impl_ctx->num_max_thread * sizeof(int8_t *) * nproc); void *nonce_chunk = @@ -378,16 +378,13 @@ static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx) goto fail; for (int i = 0; i < impl_ctx->num_max_thread; i++) { - ctx[i].work_req = - (uv_work_t *) (work_req_chunk + i * sizeof(uv_work_t) * nproc); - ctx[i].pitem = - (Pwork_struct *) (pitem_chunk + i * sizeof(Pwork_struct) * nproc); - ctx[i].nonce_array = - (int8_t **) (nonce_ptr_chunk + i * sizeof(int8_t *) * nproc); + ctx[i].work_req = (uv_work_t *) (work_req_chunk) + i * nproc; + ctx[i].pitem = (pwork_t *) (pitem_chunk) + i * nproc; + ctx[i].nonce_array = (int8_t **) (nonce_ptr_chunk) + i * nproc; for (int j = 0; j < nproc; j++) - ctx[i].nonce_array[j] = - (int8_t *) (nonce_chunk + i * NONCE_TRITS_LENGTH * nproc + - j * NONCE_TRITS_LENGTH); + ctx[i].nonce_array[j] = (int8_t *) (nonce_chunk) + + i * NONCE_TRITS_LENGTH * nproc + + j * NONCE_TRITS_LENGTH; ctx[i].num_max_threads = nproc; impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1; uv_loop_init(&ctx[i].loop); @@ -409,9 +406,9 @@ static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx) return false; } -static void PoWSSE_Context_Destroy(ImplContext *impl_ctx) +static void pow_sse_context_destroy(impl_context_t *impl_ctx) { - PoW_SSE_Context *ctx = (PoW_SSE_Context *) impl_ctx->context; + pow_sse_context_t *ctx = (pow_sse_context_t *) impl_ctx->context; for (int i = 0; i < impl_ctx->num_max_thread; i++) { uv_loop_close(&ctx[i].loop); } @@ -422,21 +419,21 @@ static void PoWSSE_Context_Destroy(ImplContext *impl_ctx) free(ctx); } -static void *PoWSSE_getPoWContext(ImplContext *impl_ctx, - int8_t *trytes, - int mwm, - int threads) +static void *pow_sse_get_pow_context(impl_context_t *impl_ctx, + int8_t *trytes, + int mwm, + int threads) { uv_mutex_lock(&impl_ctx->lock); for (int i = 0; i < impl_ctx->num_max_thread; i++) { if (impl_ctx->bitmap & (0x1 << i)) { impl_ctx->bitmap &= ~(0x1 << i); uv_mutex_unlock(&impl_ctx->lock); - PoW_SSE_Context *ctx = - impl_ctx->context + sizeof(PoW_SSE_Context) * i; + pow_sse_context_t *ctx = + (pow_sse_context_t *) impl_ctx->context + i; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = i; + ctx->index_of_context = i; if (threads > 0 && threads < ctx->num_max_threads) ctx->num_threads = threads; else @@ -448,41 +445,42 @@ static void *PoWSSE_getPoWContext(ImplContext *impl_ctx, return NULL; /* It should not happen */ } -static bool PoWSSE_freePoWContext(ImplContext *impl_ctx, void *pow_ctx) +static bool pow_sse_free_pow_context(impl_context_t *impl_ctx, void *pow_ctx) { uv_mutex_lock(&impl_ctx->lock); - impl_ctx->bitmap |= 0x1 << ((PoW_SSE_Context *) pow_ctx)->indexOfContext; + impl_ctx->bitmap |= 0x1 + << ((pow_sse_context_t *) pow_ctx)->index_of_context; uv_mutex_unlock(&impl_ctx->lock); return true; } -static int8_t *PoWSSE_getPoWResult(void *pow_ctx) +static int8_t *pow_sse_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * (TRANSACTION_TRYTES_LENGTH)); if (!ret) return NULL; - memcpy(ret, ((PoW_SSE_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_sse_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info PoWSSE_getPoWInfo(void *pow_ctx) +static pow_info_t pow_sse_get_pow_info(void *pow_ctx) { - return ((PoW_SSE_Context *) pow_ctx)->pow_info; + return ((pow_sse_context_t *) pow_ctx)->pow_info; } -ImplContext PoWSSE_Context = { +impl_context_t pow_sse_context = { .context = NULL, .description = "CPU (Intel SSE)", .bitmap = 0, .num_max_thread = 0, .num_working_thread = 0, - .initialize = PoWSSE_Context_Initialize, - .destroy = PoWSSE_Context_Destroy, - .getPoWContext = PoWSSE_getPoWContext, - .freePoWContext = PoWSSE_freePoWContext, - .doThePoW = PowSSE, - .getPoWResult = PoWSSE_getPoWResult, - .getPoWInfo = PoWSSE_getPoWInfo, + .initialize = pow_sse_context_initialize, + .destroy = pow_sse_context_destroy, + .get_pow_context = pow_sse_get_pow_context, + .free_pow_context = pow_sse_free_pow_context, + .do_the_pow = pow_sse, + .get_pow_result = pow_sse_get_pow_result, + .get_pow_info = pow_sse_get_pow_info, }; diff --git a/src/pow_sse.h b/src/pow_sse.h index 4216b59..088dce3 100644 --- a/src/pow_sse.h +++ b/src/pow_sse.h @@ -16,39 +16,39 @@ #include "trinary.h" #include "uv.h" -typedef struct _pwork_struct Pwork_struct; +typedef struct pwork_s pwork_t; -struct _pwork_struct { +struct pwork_s { int8_t *mid; int mwm; int8_t *nonce; int n; uv_rwlock_t *lock; - int *stopPoW; + int *stop_pow; int64_t ret; }; -typedef struct _pow_sse_context PoW_SSE_Context; +typedef struct pow_sse_context_s pow_sse_context_t; -struct _pow_sse_context { +struct pow_sse_context_s { /* Resource of computing */ uv_rwlock_t lock; /* Data type of libtuv */ uv_loop_t loop; uv_work_t *work_req; - Pwork_struct *pitem; + pwork_t *pitem; int8_t **nonce_array; - int stopPoW; + int stop_pow; int num_threads; int num_max_threads; /* Management of Multi-thread */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; + pow_info_t pow_info; }; #ifdef _MSC_VER @@ -59,7 +59,7 @@ struct _pow_sse_context { #define HBITS 0xFFFFFFFFFFFFFFFFuLL #define LBITS 0x0000000000000000uLL -#define INCR_START HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27 +#define INCR_START (HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH + 4 + 27) #define LOW00 \ 0xDB6DB6DB6DB6DB6DuLL // 0b1101101101101101101101101101101101101101101101101101101101101101L; #define HIGH00 \ diff --git a/src/remote_common.c b/src/remote_common.c index f5178d2..fd5e138 100644 --- a/src/remote_common.c +++ b/src/remote_common.c @@ -19,11 +19,11 @@ bool die_on_amqp_error(amqp_rpc_reply_t x, char const *context) return true; case AMQP_RESPONSE_NONE: - ddprintf("%s: missing RPC reply type!\n", context); + log_debug(0, "%s: missing RPC reply type!\n", context); break; case AMQP_RESPONSE_LIBRARY_EXCEPTION: - ddprintf("%s: %s\n", context, amqp_error_string2(x.library_error)); + log_debug(0, "%s: %s\n", context, amqp_error_string2(x.library_error)); break; case AMQP_RESPONSE_SERVER_EXCEPTION: @@ -31,21 +31,21 @@ bool die_on_amqp_error(amqp_rpc_reply_t x, char const *context) case AMQP_CONNECTION_CLOSE_METHOD: { amqp_connection_close_t *m = (amqp_connection_close_t *) x.reply.decoded; - ddprintf("%s: server connection error %u, message: %.*s\n", - context, m->reply_code, (int) m->reply_text.len, - (char *) m->reply_text.bytes); + log_debug(0, "%s: server connection error %u, message: %.*s\n", + context, m->reply_code, (int) m->reply_text.len, + (char *) m->reply_text.bytes); break; } case AMQP_CHANNEL_CLOSE_METHOD: { amqp_channel_close_t *m = (amqp_channel_close_t *) x.reply.decoded; - ddprintf("%s: server channel error %u, message: %.*s\n", context, - m->reply_code, (int) m->reply_text.len, - (char *) m->reply_text.bytes); + log_debug(0, "%s: server channel error %u, message: %.*s\n", + context, m->reply_code, (int) m->reply_text.len, + (char *) m->reply_text.bytes); break; } default: - ddprintf("%s: unknown server error, method id 0x%08X\n", context, - x.reply.id); + log_debug(0, "%s: unknown server error, method id 0x%08X\n", + context, x.reply.id); break; } break; @@ -57,23 +57,25 @@ bool die_on_amqp_error(amqp_rpc_reply_t x, char const *context) bool die_on_error(int x, char const *context) { if (x < 0) { - ddprintf("%s: %s\n", context, amqp_error_string2(x)); + log_debug(0, "%s: %s\n", context, amqp_error_string2(x)); return false; } return true; } -bool connect_broker(amqp_connection_state_t *conn, const char *hostName) +bool connect_broker(amqp_connection_state_t *conn, const char *host_name) { amqp_socket_t *socket = NULL; - const char *host = (hostName != NULL) ? hostName : "localhost"; + const char *host = (host_name != NULL) ? host_name : "localhost"; /* Connect to the rabbitmq broker */ *conn = amqp_new_connection(); socket = amqp_tcp_socket_new(*conn); - if (amqp_socket_open(socket, host, 5672) != AMQP_STATUS_OK) { - ddprintf("The rabbitmq broker of %s is closed\n", host); + if (!socket) + goto destroy_connection; + if (amqp_socket_open(socket, host, AMQP_PROTOCOL_PORT) != AMQP_STATUS_OK) { + log_debug(0, "The rabbitmq broker of %s is closed\n", host); goto destroy_connection; } @@ -131,8 +133,8 @@ bool declare_callback_queue(amqp_connection_state_t *conn, amqp_queue_declare_ok_t *r = amqp_queue_declare(*conn, channel, amqp_empty_bytes, 0, 0, 1, 0, table); - if (!die_on_amqp_error(amqp_get_rpc_reply(*conn), - "Declaring the private queue with TTL = 10s")) + if (!r || !die_on_amqp_error(amqp_get_rpc_reply(*conn), + "Declaring the private queue with TTL = 10s")) return false; *reply_to_queue = amqp_bytes_malloc_dup(r->queue); @@ -197,24 +199,25 @@ bool wait_response_message(amqp_connection_state_t *conn, if (!die_on_amqp_error(amqp_get_rpc_reply(*conn), "Wait method frame")) return false; - ddprintf(MSG_PREFIX "Frame type: %u channel: %u\n", frame.frame_type, - frame.channel); + log_debug(0, MSG_PREFIX "Frame type: %u channel: %u\n", + frame.frame_type, frame.channel); if (frame.frame_type != AMQP_FRAME_METHOD) continue; - ddprintf(MSG_PREFIX "Method: %s\n", - amqp_method_name(frame.payload.method.id)); + log_debug(0, MSG_PREFIX "Method: %s\n", + amqp_method_name(frame.payload.method.id)); if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) continue; #if defined(ENABLE_DEBUG) d = (amqp_basic_deliver_t *) frame.payload.method.decoded; - ddprintf(MSG_PREFIX "Delivery: %u exchange: %.*s routingkey: %.*s\n", - (unsigned) d->delivery_tag, (int) d->exchange.len, - (char *) d->exchange.bytes, (int) d->routing_key.len, - (char *) d->routing_key.bytes); + log_debug(0, + MSG_PREFIX "Delivery: %u exchange: %.*s routingkey: %.*s\n", + (unsigned) d->delivery_tag, (int) d->exchange.len, + (char *) d->exchange.bytes, (int) d->routing_key.len, + (char *) d->routing_key.bytes); #endif amqp_maybe_release_buffers(*conn); @@ -227,18 +230,19 @@ bool wait_response_message(amqp_connection_state_t *conn, return false; if (frame.frame_type != AMQP_FRAME_HEADER) { - ddprintf("Unexpected header!\n"); + log_debug(0, "Unexpected header!\n"); return false; } #if defined(ENABLE_DEBUG) p = (amqp_basic_properties_t *) frame.payload.properties.decoded; if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { - ddprintf(MSG_PREFIX "Content-type: %.*s\n", - (int) p->content_type.len, (char *) p->content_type.bytes); + log_debug(0, MSG_PREFIX "Content-type: %.*s\n", + (int) p->content_type.len, + (char *) p->content_type.bytes); } #endif - ddprintf("---\n"); + log_debug(0, "---\n"); body_target = (size_t) frame.payload.properties.body_size; body_received = 0; @@ -252,24 +256,24 @@ bool wait_response_message(amqp_connection_state_t *conn, return false; if (frame.frame_type != AMQP_FRAME_BODY) { - ddprintf("Unexpected body\n"); + log_debug(0, "Unexpected body\n"); return false; } body_received += frame.payload.body_fragment.len; } if (body_received != body_target) { - ddprintf("Received body is smaller than body target\n"); + log_debug(0, "Received body is smaller than body target\n"); return false; } memcpy(frame_body, (char *) frame.payload.body_fragment.bytes, body_len); - ddprintf(MSG_PREFIX "PoW result: %.*s\n", - (int) frame.payload.body_fragment.len, - (char *) frame.payload.body_fragment.bytes); - ddprintf("---\n"); + log_debug(0, MSG_PREFIX "PoW result: %.*s\n", + (int) frame.payload.body_fragment.len, + (char *) frame.payload.body_fragment.bytes); + log_debug(0, "---\n"); /* everything was fine, we can quit now because we received the reply */ return true; @@ -282,6 +286,7 @@ bool publish_message(amqp_connection_state_t *conn, char *message) { amqp_basic_properties_t props; + memset(&props, 0, sizeof(amqp_basic_properties_t)); props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = AMQP_DELIVERY_PERSISTENT; @@ -303,6 +308,7 @@ bool publish_message_with_reply_to(amqp_connection_state_t *conn, char *message) { amqp_basic_properties_t props; + memset(&props, 0, sizeof(amqp_basic_properties_t)); props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG | AMQP_BASIC_REPLY_TO_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); @@ -315,7 +321,8 @@ bool publish_message_with_reply_to(amqp_connection_state_t *conn, "Publishing the message with reply_to")) return false; - ddprintf(MSG_PREFIX "callback queue %s \n", (char *) props.reply_to.bytes); + log_debug(0, MSG_PREFIX "callback queue %s \n", + (char *) props.reply_to.bytes); amqp_bytes_free(props.reply_to); return true; diff --git a/src/remote_interface.c b/src/remote_interface.c index f9245a7..7447cf5 100644 --- a/src/remote_interface.c +++ b/src/remote_interface.c @@ -10,22 +10,33 @@ #include #include "trinary.h" -bool initializeRemoteContext(RemoteImplContext *remote_ctx) +extern struct list_head remote_impl_list; + +bool register_remote_context(remote_impl_context_t *remote_ctx) +{ + bool res = initialize_remote_context(remote_ctx); + if (res) + list_add(&remote_ctx->node, &remote_impl_list); + return res; +} + +bool initialize_remote_context(remote_impl_context_t *remote_ctx) { bool res = remote_ctx->initialize(remote_ctx); if (res) { - ddprintf(MSG_PREFIX "Implementation %s is initialized successfully\n", - remote_ctx->description); + log_debug(0, + MSG_PREFIX "Implementation %s is initialized successfully\n", + remote_ctx->description); } return res; } -void destroyRemoteContext(RemoteImplContext *remote_ctx) +void destroy_remote_context(remote_impl_context_t *remote_ctx) { return remote_ctx->destroy(remote_ctx); } -bool enterRemoteContext(RemoteImplContext *remote_ctx) +bool enter_remote_context(remote_impl_context_t *remote_ctx) { uv_mutex_lock(&remote_ctx->lock); if (remote_ctx->num_working_thread >= remote_ctx->num_max_thread) { @@ -37,51 +48,53 @@ bool enterRemoteContext(RemoteImplContext *remote_ctx) return true; /* Access Success */ } -void *getRemoteContext(RemoteImplContext *remote_ctx, int8_t *trytes, int mwm) +void *get_remote_context(remote_impl_context_t *remote_ctx, + int8_t *trytes, + int mwm) { - return remote_ctx->getPoWContext(remote_ctx, trytes, mwm); + return remote_ctx->get_pow_context(remote_ctx, trytes, mwm); } -bool doRemoteContext(RemoteImplContext *remote_ctx, void *pow_ctx) +bool do_remote_context(remote_impl_context_t *remote_ctx, void *pow_ctx) { - return remote_ctx->doThePoW(remote_ctx, pow_ctx); + return remote_ctx->do_the_pow(remote_ctx, pow_ctx); } -int8_t *getRemoteResult(RemoteImplContext *remote_ctx, void *pow_ctx) +int8_t *get_remote_result(remote_impl_context_t *remote_ctx, void *pow_ctx) { - return remote_ctx->getPoWResult(pow_ctx); + return remote_ctx->get_pow_result(pow_ctx); } -bool freeRemoteContext(RemoteImplContext *remote_ctx, void *pow_ctx) +bool free_remote_context(remote_impl_context_t *remote_ctx, void *pow_ctx) { - return remote_ctx->freePoWContext(remote_ctx, pow_ctx); + return remote_ctx->free_pow_context(remote_ctx, pow_ctx); } -void exitRemoteContext(RemoteImplContext *remote_ctx) +void exit_remote_context(remote_impl_context_t *remote_ctx) { uv_mutex_lock(&remote_ctx->lock); remote_ctx->num_working_thread--; uv_mutex_unlock(&remote_ctx->lock); } -bool PoWValidation(int8_t *output_trytes, int mwm) +bool pow_validation(int8_t *output_trytes, int mwm) { - Trytes_t *trytes_t = initTrytes(output_trytes, TRANSACTION_TRYTES_LENGTH); - if (!trytes_t) { - ddprintf("PoW Validation: Initialization of Trytes fails\n"); + trytes_t *trytes = init_trytes(output_trytes, TRANSACTION_TRYTES_LENGTH); + if (!trytes) { + log_debug(0, "PoW Validation: Initialization of Trytes fails\n"); goto fail_to_inittrytes; } - Trytes_t *hash_trytes = hashTrytes(trytes_t); - if (!hash_trytes) { - ddprintf("PoW Validation: Hashing trytes fails\n"); + trytes_t *hashed_trytes = hash_trytes(trytes); + if (!hashed_trytes) { + log_debug(0, "PoW Validation: Hashing trytes fails\n"); goto fail_to_hashtrytes; } - Trits_t *ret_trits = trits_from_trytes(hash_trytes); + trits_t *ret_trits = trits_from_trytes(hashed_trytes); for (int i = 243 - 1; i >= 243 - mwm; i--) { if (ret_trits->data[i] != 0) { - ddprintf("PoW Validation fails\n"); + log_debug(0, "PoW Validation fails\n"); goto fail_to_validation; } } @@ -89,45 +102,45 @@ bool PoWValidation(int8_t *output_trytes, int mwm) return true; fail_to_validation: - freeTrobject(ret_trits); - freeTrobject(hash_trytes); + free_trinary_object(ret_trits); + free_trinary_object(hashed_trytes); fail_to_hashtrytes: - freeTrobject(trytes_t); + free_trinary_object(trytes); fail_to_inittrytes: return false; } -static bool Remote_doPoW(RemoteImplContext *remote_ctx, void *pow_ctx) +static bool remote_do_pow(remote_impl_context_t *remote_ctx, void *pow_ctx) { char buf[4]; char messagebody[TRANSACTION_TRYTES_LENGTH + 4]; amqp_bytes_t reply_to_queue; - PoW_Remote_Context *ctx = (PoW_Remote_Context *) pow_ctx; + pow_remote_context_t *ctx = (pow_remote_context_t *) pow_ctx; /* Message body format: transacton | mwm */ memcpy(messagebody, ctx->input_trytes, TRANSACTION_TRYTES_LENGTH); snprintf(buf, sizeof(buf), "%d", ctx->mwm); memcpy(messagebody + TRANSACTION_TRYTES_LENGTH, buf, 4); - if (!declare_callback_queue(&remote_ctx->conn[ctx->indexOfContext], 1, + if (!declare_callback_queue(&remote_ctx->conn[ctx->index_of_context], 1, &reply_to_queue)) goto fail; - if (!publish_message_with_reply_to(&remote_ctx->conn[ctx->indexOfContext], + if (!publish_message_with_reply_to(&remote_ctx->conn[ctx->index_of_context], 1, "incoming_queue", reply_to_queue, messagebody)) goto fail; - if (!wait_response_message(&remote_ctx->conn[ctx->indexOfContext], 1, + if (!wait_response_message(&remote_ctx->conn[ctx->index_of_context], 1, reply_to_queue, (char *) (ctx->output_trytes), TRANSACTION_TRYTES_LENGTH)) goto fail; amqp_bytes_free(reply_to_queue); - PoWValidation(ctx->output_trytes, ctx->mwm); + pow_validation(ctx->output_trytes, ctx->mwm); return true; @@ -135,18 +148,18 @@ static bool Remote_doPoW(RemoteImplContext *remote_ctx, void *pow_ctx) return false; } -static bool Remote_init(RemoteImplContext *remote_ctx) +static bool remote_init(remote_impl_context_t *remote_ctx) { if (remote_ctx->num_max_thread <= 0) - goto fail_to_init; + goto fail_to_max_thread; - PoW_Remote_Context *ctx = (PoW_Remote_Context *) malloc( - sizeof(PoW_Remote_Context) * remote_ctx->num_max_thread); + pow_remote_context_t *ctx = (pow_remote_context_t *) malloc( + sizeof(pow_remote_context_t) * remote_ctx->num_max_thread); memset(remote_ctx->slots, 0, remote_ctx->num_max_thread * sizeof(bool)); for (int i = 0; i < CONN_MAX; i++) { - if (!connect_broker(&remote_ctx->conn[i], NULL)) + if (!connect_broker(&remote_ctx->conn[i], remote_ctx->broker_host)) goto fail_to_init; } if (!declare_queue(&remote_ctx->conn[0], 1, "incoming_queue")) @@ -159,12 +172,14 @@ static bool Remote_init(RemoteImplContext *remote_ctx) return true; fail_to_init: + free(ctx); +fail_to_max_thread: return false; } -static void Remote_destroy(RemoteImplContext *remote_ctx) +static void remote_destroy(remote_impl_context_t *remote_ctx) { - PoW_Remote_Context *ctx = (PoW_Remote_Context *) remote_ctx->context; + pow_remote_context_t *ctx = (pow_remote_context_t *) remote_ctx->context; for (int i = 0; i < CONN_MAX; i++) disconnect_broker(&remote_ctx->conn[i]); @@ -172,9 +187,9 @@ static void Remote_destroy(RemoteImplContext *remote_ctx) free(ctx); } -static void *Remote_getPoWContext(RemoteImplContext *remote_ctx, - int8_t *trytes, - int mwm) +static void *remote_get_pow_context(remote_impl_context_t *remote_ctx, + int8_t *trytes, + int mwm) { uv_mutex_lock(&remote_ctx->lock); @@ -183,11 +198,11 @@ static void *Remote_getPoWContext(RemoteImplContext *remote_ctx, remote_ctx->slots[i] = true; uv_mutex_unlock(&remote_ctx->lock); - PoW_Remote_Context *ctx = - remote_ctx->context + sizeof(PoW_Remote_Context) * i; + pow_remote_context_t *ctx = + (pow_remote_context_t *) remote_ctx->context + i; memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); ctx->mwm = mwm; - ctx->indexOfContext = i; + ctx->index_of_context = i; return ctx; } @@ -198,42 +213,44 @@ static void *Remote_getPoWContext(RemoteImplContext *remote_ctx, return NULL; /* It should not happen */ } -static bool Remote_freePoWContext(RemoteImplContext *remote_ctx, void *pow_ctx) +static bool remote_free_pow_context(remote_impl_context_t *remote_ctx, + void *pow_ctx) { uv_mutex_lock(&remote_ctx->lock); - remote_ctx->slots[((PoW_Remote_Context *) pow_ctx)->indexOfContext] = false; + remote_ctx->slots[((pow_remote_context_t *) pow_ctx)->index_of_context] = + false; uv_mutex_unlock(&remote_ctx->lock); return true; } -static int8_t *Remote_getPoWResult(void *pow_ctx) +static int8_t *remote_get_pow_result(void *pow_ctx) { int8_t *ret = (int8_t *) malloc(sizeof(int8_t) * TRANSACTION_TRYTES_LENGTH); if (!ret) return NULL; - memcpy(ret, ((PoW_Remote_Context *) pow_ctx)->output_trytes, + memcpy(ret, ((pow_remote_context_t *) pow_ctx)->output_trytes, TRANSACTION_TRYTES_LENGTH); return ret; } -static PoW_Info Remote_getPoWInfo(void *pow_ctx) +static pow_info_t remote_get_pow_info(void *pow_ctx) { - return ((PoW_Remote_Context *) pow_ctx)->pow_info; + return ((pow_remote_context_t *) pow_ctx)->pow_info; } -RemoteImplContext Remote_Context = { +remote_impl_context_t remote_context = { .context = NULL, .description = "Remote interface", .num_max_thread = CONN_MAX, // 1 <= num_max_thread .num_working_thread = 0, - .initialize = Remote_init, - .destroy = Remote_destroy, - .getPoWContext = Remote_getPoWContext, - .freePoWContext = Remote_freePoWContext, - .doThePoW = Remote_doPoW, - .getPoWResult = Remote_getPoWResult, - .getPoWInfo = Remote_getPoWInfo, + .initialize = remote_init, + .destroy = remote_destroy, + .get_pow_context = remote_get_pow_context, + .free_pow_context = remote_free_pow_context, + .do_the_pow = remote_do_pow, + .get_pow_result = remote_get_pow_result, + .get_pow_info = remote_get_pow_info, }; diff --git a/src/remote_interface.h b/src/remote_interface.h index 07c5411..e25dd88 100644 --- a/src/remote_interface.h +++ b/src/remote_interface.h @@ -13,26 +13,27 @@ #include #include "common.h" #include "constants.h" +#include "list.h" #include "remote_common.h" #include "uv.h" #define CONN_MAX 20 -typedef struct _pow_remote_context PoW_Remote_Context; -typedef struct _remote_impl_context RemoteImplContext; +typedef struct pow_remote_context_s pow_remote_context_t; +typedef struct remote_impl_context_s remote_impl_context_t; -struct _pow_remote_context { +struct pow_remote_context_s { /* Thread management */ - int indexOfContext; + int index_of_context; /* Arguments of PoW */ int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */ int mwm; /* PoW-related information */ - PoW_Info pow_info; + pow_info_t pow_info; }; -struct _remote_impl_context { +struct remote_impl_context_s { void *context; char *description; /* Connection parameters */ @@ -43,27 +44,35 @@ struct _remote_impl_context { available */ int num_max_thread; int num_working_thread; + /* RabbitMQ broker hostname */ + char *broker_host; /* Functions of Implementation Context */ - bool (*initialize)(RemoteImplContext *remote_ctx); - void (*destroy)(RemoteImplContext *remote_ctx); + bool (*initialize)(remote_impl_context_t *remote_ctx); + void (*destroy)(remote_impl_context_t *remote_ctx); /* Private PoW Context for each thread */ - void *(*getPoWContext)(RemoteImplContext *remote_ctx, - int8_t *trytes, - int mwm); - bool (*doThePoW)(RemoteImplContext *remote_ctx, void *pow_ctx); - int8_t *(*getPoWResult)(void *pow_ctx); - PoW_Info (*getPoWInfo)(void *pow_ctx); - bool (*freePoWContext)(RemoteImplContext *remote_ctx, void *pow_ctx); + void *(*get_pow_context)(remote_impl_context_t *remote_ctx, + int8_t *trytes, + int mwm); + bool (*do_the_pow)(remote_impl_context_t *remote_ctx, void *pow_ctx); + int8_t *(*get_pow_result)(void *pow_ctx); + pow_info_t (*get_pow_info)(void *pow_ctx); + bool (*free_pow_context)(remote_impl_context_t *remote_ctx, void *pow_ctx); + + /* Node in linked list */ + struct list_head node; }; -bool initializeRemoteContext(RemoteImplContext *remote_ctx); -void destroyRemoteContext(RemoteImplContext *remote_ctx); -bool enterRemoteContext(RemoteImplContext *remote_ctx); -void *getRemoteContext(RemoteImplContext *remote_ctx, int8_t *trytes, int mwm); -bool doRemoteContext(RemoteImplContext *remote_ctx, void *pow_ctx); -int8_t *getRemoteResult(RemoteImplContext *remote_ctx, void *pow_ctx); -bool freeRemoteContext(RemoteImplContext *remote_ctx, void *pow_ctx); -void exitRemoteContext(RemoteImplContext *remote_ctx); +bool register_remote_context(remote_impl_context_t *remote_ctx); +bool initialize_remote_context(remote_impl_context_t *remote_ctx); +void destroy_remote_context(remote_impl_context_t *remote_ctx); +bool enter_remote_context(remote_impl_context_t *remote_ctx); +void *get_remote_context(remote_impl_context_t *remote_ctx, + int8_t *trytes, + int mwm); +bool do_remote_context(remote_impl_context_t *remote_ctx, void *pow_ctx); +int8_t *get_remote_result(remote_impl_context_t *remote_ctx, void *pow_ctx); +bool free_remote_context(remote_impl_context_t *remote_ctx, void *pow_ctx); +void exit_remote_context(remote_impl_context_t *remote_ctx); #endif diff --git a/src/remote_worker.c b/src/remote_worker.c index d397dfa..bccfdd1 100644 --- a/src/remote_worker.c +++ b/src/remote_worker.c @@ -17,34 +17,38 @@ int main(int argc, char *const *argv) char trytes[TRANSACTION_TRYTES_LENGTH]; char buf[4]; int mwm; - int cmdOpt; - int optIdx; - const struct option longOpt[] = {{"broker", required_argument, NULL, 'b'}, - {NULL, 0, NULL, 0}}; + int cmd_opt; + int opt_idx; + const struct option long_opt[] = {{"broker", required_argument, NULL, 'b'}, + {NULL, 0, NULL, 0}}; amqp_connection_state_t conn; amqp_envelope_t envelope; - char *hostIP = NULL; + char *host = NULL; + dcurl_config config = {.broker_host = NULL}; /* Parse the command line options */ /* TODO: Support macOS since getopt_long() is GNU extension */ while (1) { - cmdOpt = getopt_long(argc, argv, "b:", longOpt, &optIdx); - if (cmdOpt == -1) + cmd_opt = getopt_long(argc, argv, "b:", long_opt, &opt_idx); + if (cmd_opt == -1) break; /* Invalid option */ - if (cmdOpt == '?') + if (cmd_opt == '?') break; - if (cmdOpt == 'b') { - hostIP = optarg; + if (cmd_opt == 'b') { + host = optarg; } } - dcurl_init(); + dcurl_init(&config); - if (!connect_broker(&conn, hostIP)) + if (!connect_broker(&conn, host)) + goto fail; + + if (!declare_queue(&conn, 1, "incoming_queue")) goto fail; if (!set_consuming_queue(&conn, 1, "incoming_queue")) @@ -54,7 +58,8 @@ int main(int argc, char *const *argv) if (!consume_message(&conn, 1, &envelope)) goto fail; - ddprintf( + log_debug( + 0, MSG_PREFIX "Delivery %u, exchange %.*s, routingkey %.*s, callback queue: %s " "\n", @@ -63,25 +68,28 @@ int main(int argc, char *const *argv) (char *) envelope.routing_key.bytes, (char *) envelope.message.properties.reply_to.bytes); if (envelope.message.properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { - ddprintf(MSG_PREFIX "Content-type: %.*s\n", - (int) envelope.message.properties.content_type.len, - (char *) envelope.message.properties.content_type.bytes); + log_debug(0, MSG_PREFIX "Content-type: %.*s\n", + (int) envelope.message.properties.content_type.len, + (char *) envelope.message.properties.content_type.bytes); } /* Message body format: transacton | mwm */ memcpy(trytes, envelope.message.body.bytes, TRANSACTION_TRYTES_LENGTH); - memcpy(buf, envelope.message.body.bytes + TRANSACTION_TRYTES_LENGTH, 4); + memcpy( + buf, + (int8_t *) envelope.message.body.bytes + TRANSACTION_TRYTES_LENGTH, + 4); mwm = strtol(buf, NULL, 10); - ddprintf(MSG_PREFIX "Doing PoW with mwm = %d...\n", mwm); + log_debug(0, MSG_PREFIX "Doing PoW with mwm = %d...\n", mwm); int8_t *ret_trytes = dcurl_entry((int8_t *) trytes, mwm, 0); memset(buf, '0', sizeof(buf)); - ddprintf(MSG_PREFIX "PoW is done\n"); + log_debug(0, MSG_PREFIX "PoW is done\n"); if (!acknowledge_broker(&conn, 1, &envelope)) goto fail; - ddprintf(MSG_PREFIX "Sending an ack is done\n"); + log_debug(0, MSG_PREFIX "Sending an ack is done\n"); /* Publish a message of remote PoW result */ if (!publish_message( @@ -91,9 +99,9 @@ int main(int argc, char *const *argv) free(ret_trytes); amqp_destroy_envelope(&envelope); - ddprintf(MSG_PREFIX - "Publishing PoW result to callback queue is done\n"); - ddprintf(MSG_PREFIX "---\n"); + log_debug( + 0, MSG_PREFIX "Publishing PoW result to callback queue is done\n"); + log_debug(0, MSG_PREFIX "---\n"); } fail: diff --git a/src/trinary.c b/src/trinary.c index 17187cc..8370e33 100644 --- a/src/trinary.c +++ b/src/trinary.c @@ -14,14 +14,14 @@ #include "constants.h" #include "curl.h" -static int8_t TrytesToTritsMappings[][3] = { +int8_t trytes_to_trits_mappings[][3] = { {0, 0, 0}, {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 1}, {-1, 1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, -1, -1}, {0, -1, -1}, {1, -1, -1}, {-1, 0, -1}, {0, 0, -1}, {1, 0, -1}, {-1, 1, -1}, {0, 1, -1}, {1, 1, -1}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}, {-1, 0, 0}}; -void freeTrobject(Trobject_t *t) +void free_trinary_object(trinary_object_t *t) { if (t) { if (t->data) @@ -30,13 +30,13 @@ void freeTrobject(Trobject_t *t) } } -static bool validateTrits(Trobject_t *trits) +static bool validate_trits(trinary_object_t *trits) { if (trits->type != TYPE_TRITS) return false; #if defined(__SSE4_2__) || defined(__ARM_NEON) - return validateTrits_sse42(trits); + return validate_trits_sse42(trits); #endif for (int i = 0; i < trits->len; i++) if (trits->data[i] < -1 || trits->data[i] > 1) @@ -44,13 +44,13 @@ static bool validateTrits(Trobject_t *trits) return true; } -static bool validateTrytes(Trobject_t *trytes) +static bool validate_trytes(trinary_object_t *trytes) { if (trytes->type != TYPE_TRYTES) return false; #if defined(__SSE4_2__) - return validateTrytes_sse42(trytes); + return validate_trytes_sse42(trytes); #endif for (int i = 0; i < trytes->len; i++) if ((trytes->data[i] < 'A' || trytes->data[i] > 'Z') && @@ -59,11 +59,11 @@ static bool validateTrytes(Trobject_t *trytes) return true; } -Trobject_t *initTrits(int8_t *src, int len) +trinary_object_t *init_trits(int8_t *src, int len) { - Trobject_t *trits = NULL; + trinary_object_t *trits; - trits = (Trobject_t *) malloc(sizeof(Trobject_t)); + trits = (trinary_object_t *) malloc(sizeof(trinary_object_t)); if (!trits) return NULL; @@ -81,8 +81,8 @@ Trobject_t *initTrits(int8_t *src, int len) trits->data[len] = '\0'; /* Check validation */ - if (!validateTrits(trits)) { - freeTrobject(trits); + if (!validate_trits(trits)) { + free_trinary_object(trits); /* Not availabe src */ return NULL; } @@ -90,11 +90,11 @@ Trobject_t *initTrits(int8_t *src, int len) return trits; } -Trobject_t *initTrytes(int8_t *src, int len) +trinary_object_t *init_trytes(int8_t *src, int len) { - Trobject_t *trytes = NULL; + trinary_object_t *trytes; - trytes = (Trobject_t *) malloc(sizeof(Trobject_t)); + trytes = (trinary_object_t *) malloc(sizeof(trinary_object_t)); if (!trytes) { return NULL; } @@ -113,8 +113,8 @@ Trobject_t *initTrytes(int8_t *src, int len) trytes->data[len] = '\0'; /* Check validation */ - if (!validateTrytes(trytes)) { - freeTrobject(trytes); + if (!validate_trytes(trytes)) { + free_trinary_object(trytes); /* Not available src */ return NULL; } @@ -122,13 +122,13 @@ Trobject_t *initTrytes(int8_t *src, int len) return trytes; } -Trobject_t *trytes_from_trits(Trobject_t *trits) +trinary_object_t *trytes_from_trits(trinary_object_t *trits) { if (!trits) { return NULL; } - if (trits->len % 3 != 0 || !validateTrits(trits)) { + if (trits->len % 3 != 0 || !validate_trits(trits)) { /* Not available trits to convert */ return NULL; } @@ -136,7 +136,7 @@ Trobject_t *trytes_from_trits(Trobject_t *trits) #if defined(__SSE4_2__) return trytes_from_trits_sse42(trits); #endif - Trobject_t *trytes = NULL; + trinary_object_t *trytes = NULL; int8_t *src = (int8_t *) malloc(trits->len / 3); /* Start converting */ @@ -146,21 +146,21 @@ Trobject_t *trytes_from_trits(Trobject_t *trits) if (j < 0) j += 27; - src[i] = TryteAlphabet[j]; + src[i] = tryte_alphabet[j]; } - trytes = initTrytes(src, trits->len / 3); + trytes = init_trytes(src, trits->len / 3); free(src); return trytes; } -Trobject_t *trits_from_trytes(Trobject_t *trytes) +trinary_object_t *trits_from_trytes(trinary_object_t *trytes) { if (!trytes) return NULL; - if (!validateTrytes(trytes)) { + if (!validate_trytes(trytes)) { /* trytes is not available to convert */ return NULL; } @@ -168,39 +168,39 @@ Trobject_t *trits_from_trytes(Trobject_t *trytes) #if defined(__SSE4_2__) return trits_from_trytes_sse42(trytes); #endif - Trobject_t *trits = NULL; + trinary_object_t *trits = NULL; int8_t *src = (int8_t *) malloc(trytes->len * 3); /* Start converting */ for (int i = 0; i < trytes->len; i++) { int idx = (trytes->data[i] == '9') ? 0 : trytes->data[i] - 'A' + 1; for (int j = 0; j < 3; j++) { - src[i * 3 + j] = TrytesToTritsMappings[idx][j]; + src[i * 3 + j] = trytes_to_trits_mappings[idx][j]; } } - trits = initTrits(src, trytes->len * 3); + trits = init_trits(src, trytes->len * 3); free(src); return trits; } -Trobject_t *hashTrytes(Trobject_t *t) +trinary_object_t *hash_trytes(trinary_object_t *t) { if (t->type != TYPE_TRYTES) return NULL; - Curl *c = initCurl(); + curl_t *c = init_curl(); if (!c) return NULL; - Absorb(c, t); - Trobject_t *ret = Squeeze(c); + absorb(c, t); + trinary_object_t *ret = squeeze(c); - freeCurl(c); + free_curl(c); return ret; } -bool compareTrobject(Trobject_t *a, Trobject_t *b) +bool compare_trinary_object(trinary_object_t *a, trinary_object_t *b) { if (a->type != b->type || a->len != b->len) return false; diff --git a/src/trinary.h b/src/trinary.h index f01cffc..7e23104 100644 --- a/src/trinary.h +++ b/src/trinary.h @@ -18,23 +18,23 @@ #define TYPE_TRITS 1 #define TYPE_TRYTES 2 -typedef struct _trinary_object { +typedef struct trinary_object_s { int8_t *data; int len; int type; -} Trobject_t; +} trinary_object_t; -typedef Trobject_t Trits_t; -typedef Trobject_t Trytes_t; +typedef trinary_object_t trits_t; +typedef trinary_object_t trytes_t; -Trobject_t *initTrits(int8_t *src, int len); -Trobject_t *initTrytes(int8_t *src, int len); +trinary_object_t *init_trits(int8_t *src, int len); +trinary_object_t *init_trytes(int8_t *src, int len); -Trobject_t *trytes_from_trits(Trobject_t *trits); -Trobject_t *trits_from_trytes(Trobject_t *trytes); -Trobject_t *hashTrytes(Trobject_t *t); +trinary_object_t *trytes_from_trits(trinary_object_t *trits); +trinary_object_t *trits_from_trytes(trinary_object_t *trytes); +trinary_object_t *hash_trytes(trinary_object_t *t); -bool compareTrobject(Trobject_t *a, Trobject_t *b); -void freeTrobject(Trobject_t *t); +bool compare_trinary_object(trinary_object_t *a, trinary_object_t *b); +void free_trinary_object(trinary_object_t *t); #endif diff --git a/src/trinary_sse42.h b/src/trinary_sse42.h index e379d06..a271c74 100644 --- a/src/trinary_sse42.h +++ b/src/trinary_sse42.h @@ -42,26 +42,28 @@ #define REPEAT11(str) REPEAT10(str), str #define REPEAT(n, str) REPEAT##n(str) -static inline bool validateTrits_sse42(Trobject_t *trits) +extern int8_t trytes_to_trits_mappings[][3]; + +static inline bool validate_trits_sse42(trinary_object_t *trits) { const int block_8bit = BLOCK_8BIT(__m128i); - const int posOneElement = 0x01010101; - const int negOneElement = 0xFFFFFFFF; - const __m128i posOne = _mm_set_epi32(posOneElement, posOneElement, - posOneElement, posOneElement); - const __m128i negOne = _mm_set_epi32(negOneElement, negOneElement, - negOneElement, negOneElement); + const int pos_one_element = 0x01010101; + const int neg_one_element = 0xFFFFFFFF; + const __m128i pos_one = _mm_set_epi32(pos_one_element, pos_one_element, + pos_one_element, pos_one_element); + const __m128i neg_one = _mm_set_epi32(neg_one_element, neg_one_element, + neg_one_element, neg_one_element); /* The for loop handles the group of the 128-bit characters without the * end-of-string */ for (int i = 0; i < (trits->len) / block_8bit; i++) { __m128i data = _mm_loadu_si128((__m128i *) (trits->data) + i); __m128i result = _mm_or_si128( /* > 1 */ - _mm_cmpgt_epi8(data, posOne), + _mm_cmpgt_epi8(data, pos_one), /* < -1 */ - _mm_cmplt_epi8(data, negOne)); - int notValid = !_mm_test_all_zeros(result, result); - if (notValid) + _mm_cmplt_epi8(data, neg_one)); + int not_valid = !_mm_test_all_zeros(result, result); + if (not_valid) return false; } /* The for loop handles the rest of the characters until the end-of-string @@ -75,7 +77,7 @@ static inline bool validateTrits_sse42(Trobject_t *trits) } #if defined(__SSE4_2__) -static inline bool validateTrytes_sse42(Trobject_t *trytes) +static inline bool validate_trytes_sse42(trinary_object_t *trytes) { const int block_8bit = BLOCK_8BIT(__m128i); /* Characters from 'A' to 'Z' and '9' to '9' */ @@ -88,15 +90,15 @@ static inline bool validateTrytes_sse42(Trobject_t *trytes) /* Check whether the characters are in the defined range or not * Return 0 if all the characters are in the range, otherwise return 1 */ - int notValid = _mm_cmpistrc(pattern, src, - /* Signed byte comparison */ - _SIDD_SBYTE_OPS | - /* Compare with the character range */ - _SIDD_CMP_RANGES | - /* Negate the comparison result */ - _SIDD_MASKED_NEGATIVE_POLARITY); + int not_valid = _mm_cmpistrc(pattern, src, + /* Signed byte comparison */ + _SIDD_SBYTE_OPS | + /* Compare with the character range */ + _SIDD_CMP_RANGES | + /* Negate the comparison result */ + _SIDD_MASKED_NEGATIVE_POLARITY); - if (notValid) + if (not_valid) return false; } /* The for loop handles the rest of the characters until the end-of-string @@ -110,145 +112,148 @@ static inline bool validateTrytes_sse42(Trobject_t *trytes) return true; } -static inline Trobject_t *trytes_from_trits_sse42(Trobject_t *trits) +static inline trinary_object_t *trytes_from_trits_sse42(trinary_object_t *trits) { - Trobject_t *trytes = NULL; + trinary_object_t *trytes = NULL; int8_t *src = (int8_t *) malloc(trits->len / 3); const int block_8bit = BLOCK_8BIT(__m128i); - const int8_t setMSB = 0x80; - const __m128i tryteAlphabet[2] = { - _mm_setr_epi8(TryteAlphabet[0], TryteAlphabet[1], TryteAlphabet[2], - TryteAlphabet[3], TryteAlphabet[4], TryteAlphabet[5], - TryteAlphabet[6], TryteAlphabet[7], TryteAlphabet[8], - TryteAlphabet[9], TryteAlphabet[10], TryteAlphabet[11], - TryteAlphabet[12], TryteAlphabet[13], TryteAlphabet[14], - TryteAlphabet[15]), - _mm_setr_epi8(TryteAlphabet[16], TryteAlphabet[17], TryteAlphabet[18], - TryteAlphabet[19], TryteAlphabet[20], TryteAlphabet[21], - TryteAlphabet[22], TryteAlphabet[23], TryteAlphabet[24], - TryteAlphabet[25], TryteAlphabet[26], 0, 0, 0, 0, 0)}; + const int8_t set_msb = 0x80; + const __m128i tryte_alphabet_for_simd[2] = { + _mm_setr_epi8(tryte_alphabet[0], tryte_alphabet[1], tryte_alphabet[2], + tryte_alphabet[3], tryte_alphabet[4], tryte_alphabet[5], + tryte_alphabet[6], tryte_alphabet[7], tryte_alphabet[8], + tryte_alphabet[9], tryte_alphabet[10], tryte_alphabet[11], + tryte_alphabet[12], tryte_alphabet[13], tryte_alphabet[14], + tryte_alphabet[15]), + _mm_setr_epi8(tryte_alphabet[16], tryte_alphabet[17], tryte_alphabet[18], + tryte_alphabet[19], tryte_alphabet[20], tryte_alphabet[21], + tryte_alphabet[22], tryte_alphabet[23], tryte_alphabet[24], + tryte_alphabet[25], tryte_alphabet[26], 0, 0, 0, 0, 0)}; /* For shuffling the bytes of the input trits */ - const __m128i shuffleLow[3] = { - _mm_setr_epi8(REPEAT(0, setMSB) COMMA(0) INDEX_3DIFF_0F COMMA(1) - REPEAT(10, setMSB)), - _mm_setr_epi8(REPEAT(6, setMSB) COMMA(1) INDEX_3DIFF_2E COMMA(1) - REPEAT(5, setMSB)), - _mm_setr_epi8(REPEAT(11, setMSB) COMMA(1) INDEX_3DIFF_1D COMMA(0) - REPEAT(0, setMSB))}; - const __m128i shuffleMid[3] = { - _mm_setr_epi8(REPEAT(0, setMSB) COMMA(0) INDEX_3DIFF_1D COMMA(1) - REPEAT(11, setMSB)), - _mm_setr_epi8(REPEAT(5, setMSB) COMMA(1) INDEX_3DIFF_0F COMMA(1) - REPEAT(5, setMSB)), - _mm_setr_epi8(REPEAT(11, setMSB) COMMA(1) INDEX_3DIFF_2E COMMA(0) - REPEAT(0, setMSB))}; - const __m128i shuffleHigh[3] = { - _mm_setr_epi8(REPEAT(0, setMSB) COMMA(0) INDEX_3DIFF_2E COMMA(1) - REPEAT(11, setMSB)), - _mm_setr_epi8(REPEAT(5, setMSB) COMMA(1) INDEX_3DIFF_1D COMMA(1) - REPEAT(6, setMSB)), - _mm_setr_epi8(REPEAT(10, setMSB) COMMA(1) INDEX_3DIFF_0F COMMA(0) - REPEAT(0, setMSB))}; + const __m128i shuffle_low[3] = { + _mm_setr_epi8(REPEAT(0, set_msb) COMMA(0) INDEX_3DIFF_0F COMMA(1) + REPEAT(10, set_msb)), + _mm_setr_epi8(REPEAT(6, set_msb) COMMA(1) INDEX_3DIFF_2E COMMA(1) + REPEAT(5, set_msb)), + _mm_setr_epi8(REPEAT(11, set_msb) COMMA(1) INDEX_3DIFF_1D COMMA(0) + REPEAT(0, set_msb))}; + const __m128i shuffle_mid[3] = { + _mm_setr_epi8(REPEAT(0, set_msb) COMMA(0) INDEX_3DIFF_1D COMMA(1) + REPEAT(11, set_msb)), + _mm_setr_epi8(REPEAT(5, set_msb) COMMA(1) INDEX_3DIFF_0F COMMA(1) + REPEAT(5, set_msb)), + _mm_setr_epi8(REPEAT(11, set_msb) COMMA(1) INDEX_3DIFF_2E COMMA(0) + REPEAT(0, set_msb))}; + const __m128i shuffle_high[3] = { + _mm_setr_epi8(REPEAT(0, set_msb) COMMA(0) INDEX_3DIFF_2E COMMA(1) + REPEAT(11, set_msb)), + _mm_setr_epi8(REPEAT(5, set_msb) COMMA(1) INDEX_3DIFF_1D COMMA(1) + REPEAT(6, set_msb)), + _mm_setr_epi8(REPEAT(10, set_msb) COMMA(1) INDEX_3DIFF_0F COMMA(0) + REPEAT(0, set_msb))}; /* The mask with interleaved bytes of 0xFF and 0x00 */ - const __m128i byteInterMask = + const __m128i byte_inter_mask = _mm_set_epi32(0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00); /* Start converting */ for (int i = 0; i < trits->len / 3 / block_8bit; i++) { /* Get trit data */ - __m128i dataFirst = _mm_loadu_si128((__m128i *) (trits->data) + i * 3); - __m128i dataMid = + __m128i data_first = _mm_loadu_si128((__m128i *) (trits->data) + i * 3); + __m128i data_mid = _mm_loadu_si128((__m128i *) (trits->data) + i * 3 + 1); - __m128i dataLast = + __m128i data_last = _mm_loadu_si128((__m128i *) (trits->data) + i * 3 + 2); /* * Each block represents a trit. - * shuffle - * ---------------- ------ ------ ------ ------ - * dataFirst = | a1 | a2 | a3 | ...... | f1 | lowTrit = | a1 | ... | f1 | ... | p1 | - * ---------------- ------ ------ ------ ------ - * ---------------- ------ ------ ------ ------ - * dataMid = | f2 | f3 | g1 | ...... | k2 | => midTrit = | a2 | ... | f2 | ... | p2 | - * ---------------- ------ ------ ------ ------ - * ---------------- ------ ------ ------ ------ - * dataLast = | k3 | l1 | l2 | ...... | p3 | highTrit = | a3 | ... | f3 | ... | p3 | - * ---------------- ------ ------ ------ ------ + * shuffle + * ---------------- ------ ------ ------ ------ + * data_first = | a1 | a2 | a3 | ...... | f1 | low_trit = | a1 | ... | f1 | ... | p1 | + * ---------------- ------ ------ ------ ------ + * ---------------- ------ ------ ------ ------ + * data_mid = | f2 | f3 | g1 | ...... | k2 | => mid_trit = | a2 | ... | f2 | ... | p2 | + * ---------------- ------ ------ ------ ------ + * ---------------- ------ ------ ------ ------ + * data_last = | k3 | l1 | l2 | ...... | p3 | high_trit = | a3 | ... | f3 | ... | p3 | + * ---------------- ------ ------ ------ ------ */ - __m128i lowTrit = _mm_or_si128( - _mm_shuffle_epi8(dataFirst, shuffleLow[0]), - _mm_or_si128(_mm_shuffle_epi8(dataMid, shuffleLow[1]), - _mm_shuffle_epi8(dataLast, shuffleLow[2]))); - __m128i midTrit = _mm_or_si128( - _mm_shuffle_epi8(dataFirst, shuffleMid[0]), - _mm_or_si128(_mm_shuffle_epi8(dataMid, shuffleMid[1]), - _mm_shuffle_epi8(dataLast, shuffleMid[2]))); - __m128i highTrit = _mm_or_si128( - _mm_shuffle_epi8(dataFirst, shuffleHigh[0]), - _mm_or_si128(_mm_shuffle_epi8(dataMid, shuffleHigh[1]), - _mm_shuffle_epi8(dataLast, shuffleHigh[2]))); - /* lowResult = (lowTrit) */ - __m128i lowResult = lowTrit; - /* midResult = (midTrit * 3) */ - __m128i midResult = _mm_or_si128( + __m128i low_trit = _mm_or_si128( + _mm_shuffle_epi8(data_first, shuffle_low[0]), + _mm_or_si128(_mm_shuffle_epi8(data_mid, shuffle_low[1]), + _mm_shuffle_epi8(data_last, shuffle_low[2]))); + __m128i mid_trit = _mm_or_si128( + _mm_shuffle_epi8(data_first, shuffle_mid[0]), + _mm_or_si128(_mm_shuffle_epi8(data_mid, shuffle_mid[1]), + _mm_shuffle_epi8(data_last, shuffle_mid[2]))); + __m128i high_trit = _mm_or_si128( + _mm_shuffle_epi8(data_first, shuffle_high[0]), + _mm_or_si128(_mm_shuffle_epi8(data_mid, shuffle_high[1]), + _mm_shuffle_epi8(data_last, shuffle_high[2]))); + /* low_result = (low_trit) */ + __m128i low_result = low_trit; + /* mid_result = (mid_trit * 3) */ + __m128i mid_result = _mm_or_si128( _mm_and_si128( - byteInterMask, - _mm_mullo_epi16(_mm_and_si128(midTrit, byteInterMask), + byte_inter_mask, + _mm_mullo_epi16(_mm_and_si128(mid_trit, byte_inter_mask), _mm_set_epi16(0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003))), _mm_andnot_si128( - byteInterMask, + byte_inter_mask, _mm_mullo_epi16( - _mm_and_si128(midTrit, ~byteInterMask), + _mm_and_si128(mid_trit, ~byte_inter_mask), _mm_set_epi16(0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003)))); - /* highResult = (highTrit * 9) */ - __m128i highResult = _mm_or_si128( + /* high_result = (high_trit * 9) */ + __m128i high_result = _mm_or_si128( _mm_and_si128( - byteInterMask, - _mm_mullo_epi16(_mm_and_si128(highTrit, byteInterMask), + byte_inter_mask, + _mm_mullo_epi16(_mm_and_si128(high_trit, byte_inter_mask), _mm_set_epi16(0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009))), _mm_andnot_si128( - byteInterMask, + byte_inter_mask, _mm_mullo_epi16( - _mm_and_si128(highTrit, ~byteInterMask), + _mm_and_si128(high_trit, ~byte_inter_mask), _mm_set_epi16(0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009)))); - /* alphabetOffset = (lowResult + midResult + highResult) */ - __m128i alphabetOffset = - _mm_add_epi8(lowResult, _mm_add_epi8(midResult, highResult)); + /* alphabet_offset = (low_result + mid_result + high_result) */ + __m128i alphabet_offset = + _mm_add_epi8(low_result, _mm_add_epi8(mid_result, high_result)); /* Check whether the offset is < 0 */ - __m128i tmpMask = - _mm_cmplt_epi8(alphabetOffset, _mm_set_epi32(0, 0, 0, 0)); + __m128i tmp_mask = + _mm_cmplt_epi8(alphabet_offset, _mm_set_epi32(0, 0, 0, 0)); /* If the offset is < 0, then offset += 27 */ - __m128i alphabetOffsetAdd = _mm_add_epi8( - alphabetOffset, + __m128i alphabet_offset_add = _mm_add_epi8( + alphabet_offset, _mm_set_epi32(0x1B1B1B1B, 0x1B1B1B1B, 0x1B1B1B1B, 0x1B1B1B1B)); - alphabetOffset = - _mm_or_si128(_mm_and_si128(tmpMask, alphabetOffsetAdd), - _mm_andnot_si128(tmpMask, alphabetOffset)); + alphabet_offset = + _mm_or_si128(_mm_and_si128(tmp_mask, alphabet_offset_add), + _mm_andnot_si128(tmp_mask, alphabet_offset)); /* Assign tryte alphabet */ /* If the offset is >= 16 (> 15), then the compared result byte = 0xFF, * else = 0x00 */ - __m128i cmpResult = _mm_cmpgt_epi8( - alphabetOffset, _mm_set_epi8(15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15)); - /* Use the offset to get the correct tryte alphabet from tryteAlphabet[] + __m128i cmp_result = _mm_cmpgt_epi8( + alphabet_offset, _mm_set_epi8(15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15)); + /* Use the offset to get the correct tryte alphabet from + * tryte_alphabet_for_simd[] */ - __m128i resultLt = _mm_shuffle_epi8(tryteAlphabet[0], alphabetOffset); - __m128i resultGe = _mm_shuffle_epi8( - tryteAlphabet[1], - /* alphabetOffset - 16 */ - _mm_sub_epi8(alphabetOffset, + __m128i result_lt = + _mm_shuffle_epi8(tryte_alphabet_for_simd[0], alphabet_offset); + __m128i result_ge = _mm_shuffle_epi8( + tryte_alphabet_for_simd[1], + /* alphabet_offset - 16 */ + _mm_sub_epi8(alphabet_offset, _mm_set_epi8(16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16))); - __m128i result = _mm_or_si128(_mm_andnot_si128(cmpResult, resultLt), - _mm_and_si128(cmpResult, resultGe)); + __m128i result = _mm_or_si128(_mm_andnot_si128(cmp_result, result_lt), + _mm_and_si128(cmp_result, result_ge)); /* Store the tryte result */ _mm_store_si128((__m128i *) (src + i * block_8bit), result); } + /* The rest of the trits */ for (int i = ((trits->len) / 3 / block_8bit) * block_8bit; i < trits->len / 3; i++) { int j = trits->data[i * 3] + trits->data[i * 3 + 1] * 3 + @@ -256,103 +261,97 @@ static inline Trobject_t *trytes_from_trits_sse42(Trobject_t *trits) if (j < 0) j += 27; - src[i] = TryteAlphabet[j]; + src[i] = tryte_alphabet[j]; } - trytes = initTrytes(src, trits->len / 3); + trytes = init_trytes(src, trits->len / 3); free(src); return trytes; } -static inline Trobject_t *trits_from_trytes_sse42(Trobject_t *trytes) +static inline trinary_object_t *trits_from_trytes_sse42(trinary_object_t *trytes) { - Trobject_t *trits = NULL; + trinary_object_t *trits = NULL; int8_t *src = (int8_t *) malloc(trytes->len * 3); const int block_8bit = BLOCK_8BIT(__m128i); /* For setting the most significant bit of a byte */ - const int8_t setMSB = 0x80; - static int8_t TrytesToTritsMappings[][3] = { - {0, 0, 0}, {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, - {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}, {-1, 0, 1}, {0, 0, 1}, - {1, 0, 1}, {-1, 1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, -1, -1}, - {0, -1, -1}, {1, -1, -1}, {-1, 0, -1}, {0, 0, -1}, {1, 0, -1}, - {-1, 1, -1}, {0, 1, -1}, {1, 1, -1}, {-1, -1, 0}, {0, -1, 0}, - {1, -1, 0}, {-1, 0, 0}}; + const int8_t set_msb = 0x80; /* The set and range for indicating the trits value (0, 1, -1) * of the corresponding trytes */ /* '9', 'C', 'F', 'I', 'L', 'O', 'R', 'U', 'X' */ - const char setLowTrit0[BYTE_OF_128BIT] = "9CFILORUX"; + const char set_low_trit_0[BYTE_OF_128BIT] = "9CFILORUX"; /* 'A', 'D', 'G', 'J', 'M', 'P', 'S', 'V', 'Y' */ - const char setLowTritP1[BYTE_OF_128BIT] = "ADGJMPSVY"; + const char set_low_trit_p1[BYTE_OF_128BIT] = "ADGJMPSVY"; /* 'B', 'E', 'H', 'K', 'N', 'Q', 'T', 'W', 'Z' */ - const char setLowTritN1[BYTE_OF_128BIT] = "BEHKNQTWZ"; + const char set_low_trit_n1[BYTE_OF_128BIT] = "BEHKNQTWZ"; /* '9', 'A', 'H', 'I', 'J', 'Q', 'R', 'S', 'Z' */ - const char rangeMidTrit0[BYTE_OF_128BIT] = "99AAHJQSZZ"; + const char range_mid_trit_0[BYTE_OF_128BIT] = "99AAHJQSZZ"; /* 'B', 'C', 'D', 'K', 'L', 'M', 'T', 'U', 'V' */ - const char rangeMidTritP1[BYTE_OF_128BIT] = "BDKMTV"; + const char range_mid_trit_p1[BYTE_OF_128BIT] = "BDKMTV"; /* 'E', 'F', 'G', 'N', 'O', 'P', 'W', 'X', 'Y' */ - const char rangeMidTritN1[BYTE_OF_128BIT] = "EGNPWY"; + const char range_mid_trit_n1[BYTE_OF_128BIT] = "EGNPWY"; /* '9', 'A', 'B', 'C', 'D', 'W', 'X', 'Y', 'Z' */ - const char rangeHighTrit0[BYTE_OF_128BIT] = "99ADWZ"; + const char range_high_trit_0[BYTE_OF_128BIT] = "99ADWZ"; /* 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' */ - const char rangeHighTritP1[BYTE_OF_128BIT] = "EM"; + const char range_high_trit_p1[BYTE_OF_128BIT] = "EM"; /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V' */ - const char rangeHighTritN1[BYTE_OF_128BIT] = "NV"; + const char range_high_trit_n1[BYTE_OF_128BIT] = "NV"; /* Convert the char array to the 128-bit data */ - const __m128i patternLowTrit0 = _mm_loadu_si128((__m128i *) (setLowTrit0)); - const __m128i patternLowTritP1 = - _mm_loadu_si128((__m128i *) (setLowTritP1)); - const __m128i patternLowTritN1 = - _mm_loadu_si128((__m128i *) (setLowTritN1)); - const __m128i patternMidTrit0 = - _mm_loadu_si128((__m128i *) (rangeMidTrit0)); - const __m128i patternMidTritP1 = - _mm_loadu_si128((__m128i *) (rangeMidTritP1)); - const __m128i patternMidTritN1 = - _mm_loadu_si128((__m128i *) (rangeMidTritN1)); - const __m128i patternHighTrit0 = - _mm_loadu_si128((__m128i *) (rangeHighTrit0)); - const __m128i patternHighTritP1 = - _mm_loadu_si128((__m128i *) (rangeHighTritP1)); - const __m128i patternHighTritN1 = - _mm_loadu_si128((__m128i *) (rangeHighTritN1)); + const __m128i pattern_low_trit_0 = + _mm_loadu_si128((__m128i *) (set_low_trit_0)); + const __m128i pattern_low_trit_p1 = + _mm_loadu_si128((__m128i *) (set_low_trit_p1)); + const __m128i pattern_low_trit_n1 = + _mm_loadu_si128((__m128i *) (set_low_trit_n1)); + const __m128i pattern_mid_trit_0 = + _mm_loadu_si128((__m128i *) (range_mid_trit_0)); + const __m128i pattern_mid_trit_p1 = + _mm_loadu_si128((__m128i *) (range_mid_trit_p1)); + const __m128i pattern_mid_trit_n1 = + _mm_loadu_si128((__m128i *) (range_mid_trit_n1)); + const __m128i pattern_high_trit_0 = + _mm_loadu_si128((__m128i *) (range_high_trit_0)); + const __m128i pattern_high_trit_p1 = + _mm_loadu_si128((__m128i *) (range_high_trit_p1)); + const __m128i pattern_high_trit_n1 = + _mm_loadu_si128((__m128i *) (range_high_trit_n1)); /* The 128-bit data with the repeated same bytes */ - const __m128i posOne = _mm_set1_epi8(1); - const __m128i negOne = _mm_set1_epi8(-1); + const __m128i pos_one = _mm_set1_epi8(1); + const __m128i neg_one = _mm_set1_epi8(-1); const __m128i zero = _mm_set1_epi8(0); /* For shuffling the bytes of the trits transformed from the input trytes */ - const __m128i shuffleFirst[3] = { - _mm_setr_epi8(0x00, REPEAT2(setMSB), 0x01, REPEAT2(setMSB), 0x02, - REPEAT2(setMSB), 0x03, REPEAT2(setMSB), 0x04, - REPEAT2(setMSB), 0x05), - _mm_setr_epi8(REPEAT1(setMSB), 0x00, REPEAT2(setMSB), 0x01, - REPEAT2(setMSB), 0x02, REPEAT2(setMSB), 0x03, - REPEAT2(setMSB), 0x04, REPEAT2(setMSB)), - _mm_setr_epi8(REPEAT2(setMSB), 0x00, REPEAT2(setMSB), 0x01, - REPEAT2(setMSB), 0x02, REPEAT2(setMSB), 0x03, - REPEAT2(setMSB), 0x04, REPEAT1(setMSB))}; - const __m128i shuffleMid[3] = { - _mm_setr_epi8(REPEAT2(setMSB), 0x06, REPEAT2(setMSB), 0x07, - REPEAT2(setMSB), 0x08, REPEAT2(setMSB), 0x09, - REPEAT2(setMSB), 0x0A, REPEAT1(setMSB)), - _mm_setr_epi8(0x05, REPEAT2(setMSB), 0x06, REPEAT2(setMSB), 0x07, - REPEAT2(setMSB), 0x08, REPEAT2(setMSB), 0x09, - REPEAT2(setMSB), 0x0A), - _mm_setr_epi8(REPEAT1(setMSB), 0x05, REPEAT2(setMSB), 0x06, - REPEAT2(setMSB), 0x07, REPEAT2(setMSB), 0x08, - REPEAT2(setMSB), 0x09, REPEAT2(setMSB))}; - const __m128i shuffleLast[3] = { - _mm_setr_epi8(REPEAT1(setMSB), 0x0B, REPEAT2(setMSB), 0x0C, - REPEAT2(setMSB), 0x0D, REPEAT2(setMSB), 0x0E, - REPEAT2(setMSB), 0x0F, REPEAT2(setMSB)), - _mm_setr_epi8(REPEAT2(setMSB), 0x0B, REPEAT2(setMSB), 0x0C, - REPEAT2(setMSB), 0x0D, REPEAT2(setMSB), 0x0E, - REPEAT2(setMSB), 0x0F, REPEAT1(setMSB)), - _mm_setr_epi8(0x0A, REPEAT2(setMSB), 0x0B, REPEAT2(setMSB), 0x0C, - REPEAT2(setMSB), 0x0D, REPEAT2(setMSB), 0x0E, - REPEAT2(setMSB), 0x0F)}; + const __m128i shuffle_first[3] = { + _mm_setr_epi8(0x00, REPEAT2(set_msb), 0x01, REPEAT2(set_msb), 0x02, + REPEAT2(set_msb), 0x03, REPEAT2(set_msb), 0x04, + REPEAT2(set_msb), 0x05), + _mm_setr_epi8(REPEAT1(set_msb), 0x00, REPEAT2(set_msb), 0x01, + REPEAT2(set_msb), 0x02, REPEAT2(set_msb), 0x03, + REPEAT2(set_msb), 0x04, REPEAT2(set_msb)), + _mm_setr_epi8(REPEAT2(set_msb), 0x00, REPEAT2(set_msb), 0x01, + REPEAT2(set_msb), 0x02, REPEAT2(set_msb), 0x03, + REPEAT2(set_msb), 0x04, REPEAT1(set_msb))}; + const __m128i shuffle_mid[3] = { + _mm_setr_epi8(REPEAT2(set_msb), 0x06, REPEAT2(set_msb), 0x07, + REPEAT2(set_msb), 0x08, REPEAT2(set_msb), 0x09, + REPEAT2(set_msb), 0x0A, REPEAT1(set_msb)), + _mm_setr_epi8(0x05, REPEAT2(set_msb), 0x06, REPEAT2(set_msb), 0x07, + REPEAT2(set_msb), 0x08, REPEAT2(set_msb), 0x09, + REPEAT2(set_msb), 0x0A), + _mm_setr_epi8(REPEAT1(set_msb), 0x05, REPEAT2(set_msb), 0x06, + REPEAT2(set_msb), 0x07, REPEAT2(set_msb), 0x08, + REPEAT2(set_msb), 0x09, REPEAT2(set_msb))}; + const __m128i shuffle_last[3] = { + _mm_setr_epi8(REPEAT1(set_msb), 0x0B, REPEAT2(set_msb), 0x0C, + REPEAT2(set_msb), 0x0D, REPEAT2(set_msb), 0x0E, + REPEAT2(set_msb), 0x0F, REPEAT2(set_msb)), + _mm_setr_epi8(REPEAT2(set_msb), 0x0B, REPEAT2(set_msb), 0x0C, + REPEAT2(set_msb), 0x0D, REPEAT2(set_msb), 0x0E, + REPEAT2(set_msb), 0x0F, REPEAT1(set_msb)), + _mm_setr_epi8(0x0A, REPEAT2(set_msb), 0x0B, REPEAT2(set_msb), 0x0C, + REPEAT2(set_msb), 0x0D, REPEAT2(set_msb), 0x0E, + REPEAT2(set_msb), 0x0F)}; /* Start converting */ /* The for loop handles the group of the 128-bit characters without the @@ -362,101 +361,98 @@ static inline Trobject_t *trits_from_trytes_sse42(Trobject_t *trytes) __m128i data = _mm_loadu_si128((__m128i *) (trytes->data) + i); /* The masks for setting the corresponding trits */ - __m128i maskLowTrit0 = _mm_cmpistrm( - patternLowTrit0, data, + __m128i mask_low_trit_0 = _mm_cmpistrm( + pattern_low_trit_0, data, /* Signed byte comparison */ _SIDD_SBYTE_OPS | /* Compare with the character set */ _SIDD_CMP_EQUAL_ANY | /* Expand the corrsponding bit result to byte unit */ _SIDD_UNIT_MASK); - __m128i maskLowTritP1 = _mm_cmpistrm( - patternLowTritP1, data, + __m128i mask_low_trit_p1 = _mm_cmpistrm( + pattern_low_trit_p1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_UNIT_MASK); - __m128i maskLowTritN1 = _mm_cmpistrm( - patternLowTritN1, data, + __m128i mask_low_trit_n1 = _mm_cmpistrm( + pattern_low_trit_n1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_UNIT_MASK); - __m128i maskMidTrit0 = _mm_cmpistrm( - patternMidTrit0, data, + __m128i mask_mid_trit_0 = _mm_cmpistrm( + pattern_mid_trit_0, data, /* Signed byte comparison */ _SIDD_SBYTE_OPS | /* Compare with the character range */ _SIDD_CMP_RANGES | /* Expand the corrsponding bit result to byte unit */ _SIDD_UNIT_MASK); - __m128i maskMidTritP1 = - _mm_cmpistrm(patternMidTritP1, data, + __m128i mask_mid_trit_p1 = + _mm_cmpistrm(pattern_mid_trit_p1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK); - __m128i maskMidTritN1 = - _mm_cmpistrm(patternMidTritN1, data, + __m128i mask_mid_trit_n1 = + _mm_cmpistrm(pattern_mid_trit_n1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK); - __m128i maskHighTrit0 = - _mm_cmpistrm(patternHighTrit0, data, + __m128i mask_high_trit_0 = + _mm_cmpistrm(pattern_high_trit_0, data, _SIDD_SBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK); - __m128i maskHighTritP1 = - _mm_cmpistrm(patternHighTritP1, data, + __m128i mask_high_trit_p1 = + _mm_cmpistrm(pattern_high_trit_p1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK); - __m128i maskHighTritN1 = - _mm_cmpistrm(patternHighTritN1, data, + __m128i mask_high_trit_n1 = + _mm_cmpistrm(pattern_high_trit_n1, data, _SIDD_SBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK); /* * Each block represents a trit. - * shuffle - * ------ ------ ------ ---------------- ------ - * lowTrit = | a1 | ... | f1 | ... | p1 | dataFirst = | a1 | a2 | a3 | ...... | f1 | - * ------ ------ ------ ---------------- ------ - * ------ ------ ------ ---------------- ------ - * midTrit = | a2 | ... | f2 | ... | p2 | => dataMid = | f2 | f3 | g1 | ...... | k2 | - * ------ ------ ------ ---------------- ------ - * ------ ------ ------ ---------------- ------ - * highTrit = | a3 | ... | f3 | ... | p3 | dataLast = | k3 | l1 | l2 | ...... | p3 | - * ------ ------ ------ ---------------- ------ + * shuffle + * ------ ------ ------ ---------------- ------ + * low_trit = | a1 | ... | f1 | ... | p1 | data_first = | a1 | a2 | a3 | ...... | f1 | + * ------ ------ ------ ---------------- ------ + * ------ ------ ------ ---------------- ------ + * mid_trit = | a2 | ... | f2 | ... | p2 | => data_mid = | f2 | f3 | g1 | ...... | k2 | + * ------ ------ ------ ---------------- ------ + * ------ ------ ------ ---------------- ------ + * high_trit = | a3 | ... | f3 | ... | p3 | data_last = | k3 | l1 | l2 | ...... | p3 | + * ------ ------ ------ ---------------- ------ */ - __m128i lowTrit = - _mm_or_si128(_mm_and_si128(maskLowTrit0, zero), - _mm_or_si128(_mm_and_si128(maskLowTritP1, posOne), - _mm_and_si128(maskLowTritN1, negOne))); - __m128i midTrit = - _mm_or_si128(_mm_and_si128(maskMidTrit0, zero), - _mm_or_si128(_mm_and_si128(maskMidTritP1, posOne), - _mm_and_si128(maskMidTritN1, negOne))); - __m128i highTrit = - _mm_or_si128(_mm_and_si128(maskHighTrit0, zero), - _mm_or_si128(_mm_and_si128(maskHighTritP1, posOne), - _mm_and_si128(maskHighTritN1, negOne))); - /* Initialize with 0 */ - __m128i dataFirst = _mm_set1_epi8(0); - __m128i dataMid = _mm_set1_epi8(0); - __m128i dataLast = _mm_set1_epi8(0); - dataFirst = _mm_or_si128( - _mm_shuffle_epi8(lowTrit, shuffleFirst[0]), - _mm_or_si128(_mm_shuffle_epi8(midTrit, shuffleFirst[1]), - _mm_shuffle_epi8(highTrit, shuffleFirst[2]))); - dataMid = _mm_or_si128( - _mm_shuffle_epi8(lowTrit, shuffleMid[0]), - _mm_or_si128(_mm_shuffle_epi8(midTrit, shuffleMid[1]), - _mm_shuffle_epi8(highTrit, shuffleMid[2]))); - dataLast = _mm_or_si128( - _mm_shuffle_epi8(lowTrit, shuffleLast[0]), - _mm_or_si128(_mm_shuffle_epi8(midTrit, shuffleLast[1]), - _mm_shuffle_epi8(highTrit, shuffleLast[2]))); + __m128i low_trit = _mm_or_si128( + _mm_and_si128(mask_low_trit_0, zero), + _mm_or_si128(_mm_and_si128(mask_low_trit_p1, pos_one), + _mm_and_si128(mask_low_trit_n1, neg_one))); + __m128i mid_trit = _mm_or_si128( + _mm_and_si128(mask_mid_trit_0, zero), + _mm_or_si128(_mm_and_si128(mask_mid_trit_p1, pos_one), + _mm_and_si128(mask_mid_trit_n1, neg_one))); + __m128i high_trit = _mm_or_si128( + _mm_and_si128(mask_high_trit_0, zero), + _mm_or_si128(_mm_and_si128(mask_high_trit_p1, pos_one), + _mm_and_si128(mask_high_trit_n1, neg_one))); + __m128i data_first, data_mid, data_last; + data_first = _mm_or_si128( + _mm_shuffle_epi8(low_trit, shuffle_first[0]), + _mm_or_si128(_mm_shuffle_epi8(mid_trit, shuffle_first[1]), + _mm_shuffle_epi8(high_trit, shuffle_first[2]))); + data_mid = _mm_or_si128( + _mm_shuffle_epi8(low_trit, shuffle_mid[0]), + _mm_or_si128(_mm_shuffle_epi8(mid_trit, shuffle_mid[1]), + _mm_shuffle_epi8(high_trit, shuffle_mid[2]))); + data_last = _mm_or_si128( + _mm_shuffle_epi8(low_trit, shuffle_last[0]), + _mm_or_si128(_mm_shuffle_epi8(mid_trit, shuffle_last[1]), + _mm_shuffle_epi8(high_trit, shuffle_last[2]))); /* Store the 3 * 128-bit trits converted from trytes */ - _mm_store_si128((__m128i *) (src + (3 * i) * block_8bit), dataFirst); - _mm_store_si128((__m128i *) (src + (3 * i + 1) * block_8bit), dataMid); - _mm_store_si128((__m128i *) (src + (3 * i + 2) * block_8bit), dataLast); + _mm_store_si128((__m128i *) (src + (3 * i) * block_8bit), data_first); + _mm_store_si128((__m128i *) (src + (3 * i + 1) * block_8bit), data_mid); + _mm_store_si128((__m128i *) (src + (3 * i + 2) * block_8bit), data_last); } /* The rest of the trytes */ for (int i = (trytes->len / block_8bit) * block_8bit; i < trytes->len; i++) { int idx = (trytes->data[i] == '9') ? 0 : trytes->data[i] - 'A' + 1; for (int j = 0; j < 3; j++) { - src[i * 3 + j] = TrytesToTritsMappings[idx][j]; + src[i * 3 + j] = trytes_to_trits_mappings[idx][j]; } } - trits = initTrits(src, trytes->len * 3); + trits = init_trits(src, trytes->len * 3); free(src); return trits; diff --git a/tests/common.h b/tests/common.h index 62f3ad0..804a1b4 100644 --- a/tests/common.h +++ b/tests/common.h @@ -24,8 +24,8 @@ #if defined(ENABLE_OPENCL) #include "pow_cl.h" #endif -#if defined(ENABLE_FPGA_ACCEL) -#include "pow_fpga_accel.h" +#if defined(ENABLE_FPGA) +#include "pow_fpga.h" #endif #include diff --git a/tests/test-curl.c b/tests/test-curl.c index dca9a96..33b3e64 100644 --- a/tests/test-curl.c +++ b/tests/test-curl.c @@ -52,19 +52,19 @@ int main() "SYVDPCXX9LLAT"; int length_test_case = 2673; - Trytes_t *answer = initTrytes((signed char *) "WPM9JCTQH9QHBHBJCODWDNPSLFLQDZADRAEZTZDL9OEGTVRZARVLVJHZIMPBPCTAIYJKWTUSRKNNTMFOQ", 81); + trytes_t *answer = init_trytes((signed char *) "WPM9JCTQH9QHBHBJCODWDNPSLFLQDZADRAEZTZDL9OEGTVRZARVLVJHZIMPBPCTAIYJKWTUSRKNNTMFOQ", 81); assert(answer); - Trytes_t *trytes = - initTrytes((signed char *) trytes_test_case, length_test_case); + trytes_t *trytes = + init_trytes((signed char *) trytes_test_case, length_test_case); assert(trytes); - Trytes_t *ret_trytes = hashTrytes(trytes); + trytes_t *ret_trytes = hash_trytes(trytes); assert(ret_trytes); - int ret = compareTrobject(ret_trytes, answer); + int ret = compare_trinary_object(ret_trytes, answer); - freeTrobject(answer); - freeTrobject(trytes); - freeTrobject(ret_trytes); + free_trinary_object(answer); + free_trinary_object(trytes); + free_trinary_object(ret_trytes); assert(ret != 0); diff --git a/tests/test-dcurl.c b/tests/test-dcurl.c index 3fb5a10..861a72b 100644 --- a/tests/test-dcurl.c +++ b/tests/test-dcurl.c @@ -14,7 +14,7 @@ int main() { - char *trytes = + char *transaction_trytes = "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" @@ -59,26 +59,26 @@ int main() for (int loop_count = 0; loop_count < LOOP_MAX; loop_count++) { /* test dcurl Implementation with mwm = 14 */ - dcurl_init(); - int8_t *ret_trytes = dcurl_entry((int8_t *) trytes, mwm, 8); + dcurl_init(NULL); + int8_t *ret_trytes = dcurl_entry((int8_t *) transaction_trytes, mwm, 8); assert(ret_trytes); dcurl_destroy(); - Trytes_t *trytes_t = initTrytes(ret_trytes, 2673); - assert(trytes_t); - Trytes_t *hash_trytes = hashTrytes(trytes_t); - assert(hash_trytes); + trytes_t *trytes = init_trytes(ret_trytes, 2673); + assert(trytes); + trytes_t *hashed_trytes = hash_trytes(trytes); + assert(hashed_trytes); /* Validation */ - Trits_t *ret_trits = trits_from_trytes(hash_trytes); + trits_t *ret_trits = trits_from_trytes(hashed_trytes); for (int i = 243 - 1; i >= 243 - mwm; i--) { assert(ret_trits->data[i] == 0); } free(ret_trytes); - freeTrobject(trytes_t); - freeTrobject(hash_trytes); - freeTrobject(ret_trits); + free_trinary_object(trytes); + free_trinary_object(hashed_trytes); + free_trinary_object(ret_trits); } return 0; diff --git a/tests/test-multi-pow.c b/tests/test-multi-pow.c index df77c72..1778403 100644 --- a/tests/test-multi-pow.c +++ b/tests/test-multi-pow.c @@ -34,7 +34,7 @@ void *dcurl_entry_cb(void *arg) int main() { - char *trytes = + char *transaction_trytes = "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" @@ -79,10 +79,10 @@ int main() pthread_t threads[THREAD_MAX]; dcurl_item items[THREAD_MAX]; - dcurl_init(); + dcurl_init(NULL); for (int i = 0; i < THREAD_MAX; i++) { - memcpy(items[i].input_trytes, trytes, TRANSACTION_TRYTES_LENGTH); + memcpy(items[i].input_trytes, transaction_trytes, TRANSACTION_TRYTES_LENGTH); items[i].mwm = mwm; pthread_create(&threads[i], NULL, dcurl_entry_cb, (void *) &items[i]); } @@ -91,21 +91,21 @@ int main() pthread_join(threads[i], NULL); for (int i = 0; i < THREAD_MAX; i++) { - Trytes_t *trytes_t = - initTrytes(items[i].output_trytes, TRANSACTION_TRYTES_LENGTH); - assert(trytes_t && "initTrytes() failed"); - Trytes_t *hash_trytes = hashTrytes(trytes_t); - assert(hash_trytes && "hashTrytes() failed"); + trytes_t *trytes = + init_trytes(items[i].output_trytes, TRANSACTION_TRYTES_LENGTH); + assert(trytes && "init_trytes() failed"); + trytes_t *hashed_trytes = hash_trytes(trytes); + assert(hashed_trytes && "hash_trytes() failed"); /* Validation */ - Trits_t *ret_trits = trits_from_trytes(hash_trytes); + trits_t *ret_trits = trits_from_trytes(hashed_trytes); for (int j = 243 - 1; j >= 243 - items[i].mwm; j--) { assert(ret_trits->data[j] == 0 && "Validation failed"); } - freeTrobject(trytes_t); - freeTrobject(hash_trytes); - freeTrobject(ret_trits); + free_trinary_object(trytes); + free_trinary_object(hashed_trytes); + free_trinary_object(ret_trits); } dcurl_destroy(); diff --git a/tests/test-pow.c b/tests/test-pow.c index 9666914..9becc01 100644 --- a/tests/test-pow.c +++ b/tests/test-pow.c @@ -7,26 +7,26 @@ */ /* Test program for pow_*.c */ +#include #include "common.h" #include "implcontext.h" -#include "math.h" #define POW_TOTAL 100 #if defined(ENABLE_AVX) -extern ImplContext PoWAVX_Context; +extern impl_context_t pow_avx_context; #elif defined(ENABLE_SSE) -extern ImplContext PoWSSE_Context; +extern impl_context_t pow_sse_context; #elif defined(ENABLE_GENERIC) -extern ImplContext PoWC_Context; +extern impl_context_t pow_c_context; #endif #if defined(ENABLE_OPENCL) -extern ImplContext PoWCL_Context; +extern impl_context_t pow_cl_context; #endif -#if defined(ENABLE_FPGA_ACCEL) -extern ImplContext PoWFPGAAccel_Context; +#if defined(ENABLE_FPGA) +extern impl_context_t pow_fpga_context; #endif const char *description[] = { @@ -42,39 +42,39 @@ const char *description[] = { "GPU - OpenCL", #endif -#if defined(ENABLE_FPGA_ACCEL) +#if defined(ENABLE_FPGA) "FPGA", #endif }; -double getAvg(const double arr[], int arrLen) +double get_avg(const double arr[], int arr_len) { double avg, sum = 0; - for (int idx = 0; idx < arrLen; idx++) { + for (int idx = 0; idx < arr_len; idx++) { sum += arr[idx]; } - avg = sum / arrLen; + avg = sum / arr_len; return avg; } -double getStdDeviation(const double arr[], int arrLen) +double get_std_deviation(const double arr[], int arr_len) { double sigma, variance = 0; - double avg = getAvg(arr, arrLen); + double avg = get_avg(arr, arr_len); - for (int idx = 0; idx < arrLen; idx++) { + for (int idx = 0; idx < arr_len; idx++) { variance += pow(arr[idx] - avg, 2); } - sigma = sqrt(variance / arrLen); + sigma = sqrt(variance / arr_len); return sigma; } int main() { - char *trytes = + char *transaction_trytes = "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" "9999999999999999999999999999999999999999999999999999999999999999999999" @@ -117,57 +117,57 @@ int main() int mwm = 14; - ImplContext ImplContextArr[] = { + impl_context_t impl_context_arr[] = { #if defined(ENABLE_AVX) - PoWAVX_Context, + pow_avx_context, #elif defined(ENABLE_SSE) - PoWSSE_Context, + pow_sse_context, #elif defined(ENABLE_GENERIC) - PoWC_Context, + pow_c_context, #endif #if defined(ENABLE_OPENCL) - PoWCL_Context, + pow_cl_context, #endif -#if defined(ENABLE_FPGA_ACCEL) - PoWFPGAAccel_Context, +#if defined(ENABLE_FPGA) + pow_fpga_context, #endif }; #if defined(ENABLE_STAT) - double hashRateArr[POW_TOTAL]; + double hash_rate_arr[POW_TOTAL]; int pow_total = POW_TOTAL; #else int pow_total = 1; #endif - for (int idx = 0; idx < sizeof(ImplContextArr) / sizeof(ImplContext); + for (int idx = 0; idx < sizeof(impl_context_arr) / sizeof(impl_context_t); idx++) { - printf("%s\n", description[idx]); + log_info(0, "%s\n", description[idx]); - ImplContext *PoW_Context_ptr = &ImplContextArr[idx]; + impl_context_t *pow_context_ptr = &impl_context_arr[idx]; /* test implementation with mwm = 14 */ - initializeImplContext(PoW_Context_ptr); + initialize_impl_context(pow_context_ptr); void *pow_ctx = - getPoWContext(PoW_Context_ptr, (int8_t *) trytes, mwm, 8); + get_pow_context(pow_context_ptr, (int8_t *) transaction_trytes, mwm, 8); assert(pow_ctx); for (int count = 0; count < pow_total; count++) { - doThePoW(PoW_Context_ptr, pow_ctx); - int8_t *ret_trytes = getPoWResult(PoW_Context_ptr, pow_ctx); + do_the_pow(pow_context_ptr, pow_ctx); + int8_t *ret_trytes = get_pow_result(pow_context_ptr, pow_ctx); assert(ret_trytes); #if defined(ENABLE_STAT) - PoW_Info pow_info = getPoWInfo(PoW_Context_ptr, pow_ctx); + pow_info_t pow_info = get_pow_info(pow_context_ptr, pow_ctx); #endif - Trytes_t *trytes_t = - initTrytes(ret_trytes, TRANSACTION_TRYTES_LENGTH); - assert(trytes_t); - Trytes_t *hash_trytes = hashTrytes(trytes_t); - assert(hash_trytes); - Trits_t *ret_trits = trits_from_trytes(hash_trytes); + trytes_t *trytes = + init_trytes(ret_trytes, TRANSACTION_TRYTES_LENGTH); + assert(trytes); + trytes_t *hashed_trytes = hash_trytes(trytes); + assert(hashed_trytes); + trits_t *ret_trits = trits_from_trytes(hashed_trytes); assert(ret_trits); /* Validation */ @@ -177,28 +177,29 @@ int main() } free(ret_trytes); - freeTrobject(trytes_t); - freeTrobject(hash_trytes); - freeTrobject(ret_trits); + free_trinary_object(trytes); + free_trinary_object(hashed_trytes); + free_trinary_object(ret_trits); #if defined(ENABLE_STAT) - hashRateArr[count] = pow_info.hash_count / pow_info.time; + hash_rate_arr[count] = pow_info.hash_count / pow_info.time; #endif } - freePoWContext(PoW_Context_ptr, pow_ctx); - destroyImplContext(PoW_Context_ptr); + free_pow_context(pow_context_ptr, pow_ctx); + destroy_impl_context(pow_context_ptr); - printf("PoW execution times: %d times.\n", pow_total); + log_info(0, "PoW execution times: %d times.\n", pow_total); #if defined(ENABLE_STAT) - printf("Hash rate average value: %.3lf kH/sec,\n", - getAvg(hashRateArr, pow_total) / 1000); - printf( + log_info(0, "Hash rate average value: %.3lf kH/sec,\n", + get_avg(hash_rate_arr, pow_total) / 1000); + log_info( + 0, "with the range +- %.3lf kH/sec including 95%% of the hash rate " "values.\n", - 2 * getStdDeviation(hashRateArr, pow_total) / 1000); + 2 * get_std_deviation(hash_rate_arr, pow_total) / 1000); #endif - printf("Success.\n"); + log_info(0, "Success.\n"); } return 0; diff --git a/tests/test-trinary.c b/tests/test-trinary.c index b69079a..0e8ec9d 100644 --- a/tests/test-trinary.c +++ b/tests/test-trinary.c @@ -15,19 +15,19 @@ int main() "UG9OEOZPOTD"; int length_test_case = 81; - Trytes_t *trytes = - initTrytes((signed char *) trytes_test_case, length_test_case); + trytes_t *trytes = + init_trytes((signed char *) trytes_test_case, length_test_case); assert(trytes); - Trits_t *trits = trits_from_trytes(trytes); + trits_t *trits = trits_from_trytes(trytes); assert(trits); - Trytes_t *ret_trytes = trytes_from_trits(trits); + trytes_t *ret_trytes = trytes_from_trits(trits); assert(ret_trytes); - int ret = compareTrobject(trytes, ret_trytes); + int ret = compare_trinary_object(trytes, ret_trytes); - freeTrobject(trytes); - freeTrobject(trits); - freeTrobject(ret_trytes); + free_trinary_object(trytes); + free_trinary_object(trits); + free_trinary_object(ret_trytes); assert(ret != 0);