Skip to content

Commit

Permalink
Merge pull request #73 from marktwtn/hash-count
Browse files Browse the repository at this point in the history
Calculate hash count for each PoW
  • Loading branch information
jserv committed Sep 27, 2018
2 parents 5dc6479 + 86410f4 commit 8d95ec5
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 92 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ ifeq ("$(BUILD_JNI)","1")
include mk/java.mk
endif

ifeq ("$(BUILD_STAT)","1")
CFLAGS += -DENABLE_STAT
endif

TESTS = \
trinary \
curl \
Expand Down
61 changes: 52 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Reference Implementation (IRI). Additionally, dcurl also supports the FPGA-accel
downloading from [latest JAVA source](https://github.com/DLTcollab/iri).
- ``BUILD_COMPAT``: build extra cCurl compatible interface.
- ``BUILD_FPGA_ACCEL``: build the interface interacting with the Cyclone V FPGA based accelerator. Verified on DE10-nano board and Arrow SoCKit board.
- ``BUILD_STAT``: show the statistics of the PoW information.
* Alternatively, you can specify conditional build as following:
```shell
$ make BUILD_GPU=0 BUILD_JNI=1 BUILD_AVX=1
Expand All @@ -43,13 +44,15 @@ $ make BUILD_GPU=1 check
[ Verified ]
*** Validating build/test-curl ***
[ Verified ]
*** Validating build/test-multi_pow_cpu ***
[ Verified ]
*** Validating build/test-pow_sse ***
*** Validating build/test-durl ***
[dcurl] Implementation GPU (OpenCL) is initialized successfully
[ Verified ]
*** Validating build/test-pow_cl ***
*** Validating build/test-multi_pow ***
[ Verified ]
*** Validating build/test-multi_pow_gpu ***
*** Validating build/test-pow ***
GPU - OpenCL
[dcurl] Implementation GPU (OpenCL) is initialized successfully
Success.
[ Verified ]
```

Expand All @@ -64,9 +67,41 @@ $ make BUILD_AVX=1 check
[ Verified ]
*** Validating build/test-curl ***
[ Verified ]
*** Validating build/test-dcurl ***
[dcurl] Implementation CPU (Intel AVX) is initialized successfully
[ Verified ]
*** Validating build/test-multi_pow_cpu ***
[ Verified ]
*** Validating build/test-pow_avx ***
*** Validating build/test-pow ***
CPU - AVX
[dcurl] Implementation CPU (Intel AVX) is initialized successfully
Success.
[ Verified ]
```

* Test with AVX and show the PoW statistics
```shell
$ make BUILD_AVX=1 BUILD_STAT=1 check
```

* Expected Results
```
*** Validating build/test-trinary ***
[ Verified ]
*** Validating build/test-curl ***
[ Verified ]
*** Validating build/test-dcurl ***
[dcurl] Implementation CPU (Intel AVX) is initialized successfully
[ Verified ]
*** Validating build/test-multi_pow_cpu ***
[ Verified ]
*** Validating build/test-pow ***
CPU - AVX
[dcurl] Implementation CPU (Intel AVX) is initialized successfully
Hash count: 7396100
PoW execution time: 3 sec
Hash rate: 2465366.667 kH/sec
Success.
[ Verified ]
```

Expand All @@ -83,11 +118,19 @@ root@lampa:~/dcurl# make BUILD_FPGA_ACCEL=1 check
[ Verified ]
*** Validating build/test-curl ***
[ Verified ]
*** Validating build/test-pow_c ***
*** Validating build/test-dcurl ***
[dcurl] Implementation CPU (Intel SSE) is initialized successfully
[dcurl] Implementation FPGA is initialized successfully
[ Verified ]
*** Validating build/test-multi_pow_cpu ***
*** Validating build/test-multi_pow ***
[ Verified ]
*** Validating build/test-pow_fpga_accel ***
*** Validating build/test-pow ***
CPU - SSE
[dcurl] Implementation CPU (Intel SSE) is initialized successfully
Success.
FPGA
[dcurl] Implementation FPGA is initialized successfully
Success.
[ Verified ]
```

Expand Down
3 changes: 3 additions & 0 deletions mk/defs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ BUILD_COMPAT ?= 0

# Build FPGA backend or not
BUILD_FPGA_ACCEL ?= 0

# Show the PoW-related statistic messages or not
BUILD_STAT ?= 0
1 change: 1 addition & 0 deletions src/clcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct {
cl_ulong max_memory;
size_t num_work_group;
KernelInfo kernel_info;
uint64_t hash_count;
} CLContext;

enum {
Expand Down
11 changes: 11 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef COMMON_H_
#define COMMON_H_

typedef struct _pow_info PoW_Info;

struct _pow_info {
double time;
uint64_t hash_count;
};

#endif
1 change: 0 additions & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#define MinTryteValue -13
#define MaxTryteValue 13
#define SignatureSize 6561
#define HashSize 243
#define Depth 3
#define Radix 3

Expand Down
5 changes: 5 additions & 0 deletions src/implcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ int8_t *getPoWResult(ImplContext *impl_ctx, void *pow_ctx)
{
return impl_ctx->getPoWResult(pow_ctx);
}

void *getPoWInfo(ImplContext *impl_ctx, void *pow_ctx)
{
return impl_ctx->getPoWInfo(pow_ctx);
}
2 changes: 2 additions & 0 deletions src/implcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct _impl_context {
void *(*getPoWContext)(ImplContext *impl_ctx, int8_t *trytes, int mwm);
bool (*doThePoW)(void *pow_ctx);
int8_t *(*getPoWResult)(void *pow_ctx);
void *(*getPoWInfo)(void *pow_ctx);
bool (*freePoWContext)(ImplContext *impl_ctx, void *pow_ctx);

/* Linked list */
Expand All @@ -40,5 +41,6 @@ void *getPoWContext(ImplContext *impl_ctx, int8_t *trytes, int mwm);
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);
void *getPoWInfo(ImplContext *impl_ctx, void *pow_ctx);

#endif
55 changes: 36 additions & 19 deletions src/pow_avx.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,29 +487,29 @@ static int8_t *tx_to_cstate(Trytes_t *tx)
{
Trytes_t *inn = NULL;
Trits_t *tr = NULL;
int8_t tyt[(transactionTrinarySize - HashSize) / 3] = {0};
int8_t tyt[TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] = {0};

Curl *c = initCurl();
int8_t *c_state = (int8_t *) malloc(STATE_TRITS_LENGTH);
if (!c || !c_state) goto fail;

/* Copy tx->data[:(transactionTrinarySize - HashSize) / 3] to tyt */
memcpy(tyt, tx->data, (transactionTrinarySize - HashSize) / 3);
/* Copy tx->data[:TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH] to tyt */
memcpy(tyt, tx->data, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH);

inn = initTrytes(tyt, (transactionTrinarySize - HashSize) / 3);
inn = initTrytes(tyt, TRANSACTION_TRYTES_LENGTH - HASH_TRYTES_LENGTH);
if (!inn) goto fail;

Absorb(c, inn);

tr = trits_from_trytes(tx);
if (!tr) goto fail;

/* Prepare an array storing tr[transactionTrinarySize - HashSize:] */
memcpy(c_state, tr->data + transactionTrinarySize - HashSize,
tr->len - (transactionTrinarySize - HashSize));
memcpy(c_state + tr->len - (transactionTrinarySize - HashSize),
c->state->data + tr->len - (transactionTrinarySize - HashSize),
c->state->len - tr->len + (transactionTrinarySize - HashSize));
/* Prepare an array storing tr[TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH:] */
memcpy(c_state, tr->data + TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH,
tr->len - (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH));
memcpy(c_state + tr->len - (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH),
c->state->data + tr->len - (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH),
c->state->len - tr->len + (TRANSACTION_TRITS_LENGTH - HASH_TRITS_LENGTH));

freeTrobject(inn);
freeTrobject(tr);
Expand All @@ -525,22 +525,25 @@ static int8_t *tx_to_cstate(Trytes_t *tx)

static void nonce_to_result(Trytes_t *tx, Trytes_t *nonce, int8_t *ret)
{
int rst_len = tx->len - NonceTrinarySize / 3 + nonce->len;
int rst_len = tx->len - NONCE_TRYTES_LENGTH + nonce->len;

memcpy(ret, tx->data, tx->len - NonceTrinarySize / 3);
memcpy(ret + tx->len - NonceTrinarySize / 3, nonce->data,
rst_len - (tx->len - NonceTrinarySize / 3));
memcpy(ret, tx->data, tx->len - NONCE_TRYTES_LENGTH);
memcpy(ret + tx->len - NONCE_TRYTES_LENGTH, nonce->data,
rst_len - (tx->len - NONCE_TRYTES_LENGTH));
}

bool PowAVX(void *pow_ctx)
{
bool res = true;
Trits_t *nonce_trit = NULL;
Trytes_t *tx_tryte = NULL, *nonce_tryte = NULL;
time_t start_time, end_time;

/* Initialize the context */
PoW_AVX_Context *ctx = (PoW_AVX_Context *) pow_ctx;
ctx->stopPoW = 0;
ctx->pow_info->time = 0;
ctx->pow_info->hash_count = 0;
pthread_mutex_init(&ctx->lock, NULL);
pthread_t *threads = ctx->threads;
Pwork_struct *pitem = ctx->pitem;
Expand All @@ -556,6 +559,7 @@ bool PowAVX(void *pow_ctx)
goto fail;
}

time(&start_time);
/* Prepare arguments for pthread */
for (int i = 0; i < ctx->num_threads; i++) {
pitem[i].mid = c_state;
Expand All @@ -573,9 +577,12 @@ bool PowAVX(void *pow_ctx)
pthread_join(threads[i], NULL);
if (pitem[i].n == -1)
completedIndex = i;
ctx->pow_info->hash_count += (uint64_t) (pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1);
}
time(&end_time);
ctx->pow_info->time = difftime(end_time, start_time);

nonce_trit = initTrits(nonce_array[completedIndex], NonceTrinarySize);
nonce_trit = initTrits(nonce_array[completedIndex], NONCE_TRITS_LENGTH);
if (!nonce_trit) {
res = false;
goto fail;
Expand Down Expand Up @@ -611,16 +618,18 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx)
void *threads_chunk = malloc(impl_ctx->num_max_thread * sizeof(pthread_t) * nproc);
void *pitem_chunk = malloc(impl_ctx->num_max_thread * sizeof(Pwork_struct) * nproc);
void *nonce_ptr_chunk = malloc(impl_ctx->num_max_thread * sizeof(int8_t *) * nproc);
void *nonce_chunk = malloc(impl_ctx->num_max_thread * NonceTrinarySize * nproc);
if (!threads_chunk || !pitem_chunk || !nonce_ptr_chunk || !nonce_chunk) goto fail;
void *nonce_chunk = malloc(impl_ctx->num_max_thread * NONCE_TRITS_LENGTH * nproc);
void *pow_info_chunk = malloc(impl_ctx->num_max_thread * sizeof(PoW_Info));
if (!threads_chunk || !pitem_chunk || !nonce_ptr_chunk || !nonce_chunk || !pow_info_chunk) goto fail;

for (int i = 0; i < impl_ctx->num_max_thread; i++) {
ctx[i].threads = (pthread_t *) (threads_chunk + i * sizeof(pthread_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);
for (int j = 0; j < nproc; j++)
ctx[i].nonce_array[j] = (int8_t *) (nonce_chunk + i * NonceTrinarySize * nproc +
j * NonceTrinarySize);
ctx[i].nonce_array[j] = (int8_t *) (nonce_chunk + i * NONCE_TRITS_LENGTH * nproc +
j * NONCE_TRITS_LENGTH);
ctx[i].pow_info = (PoW_Info *) (pow_info_chunk + i * sizeof(PoW_Info));
ctx[i].num_threads = nproc;
impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1;
}
Expand All @@ -634,6 +643,7 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx)
free(pitem_chunk);
free(nonce_ptr_chunk);
free(nonce_chunk);
free(pow_info_chunk);
return false;
}

Expand All @@ -644,6 +654,7 @@ static void PoWAVX_Context_Destroy(ImplContext *impl_ctx)
free(ctx[0].pitem);
free(ctx[0].nonce_array[0]);
free(ctx[0].nonce_array);
free(ctx[0].pow_info);
free(ctx);
}

Expand Down Expand Up @@ -681,6 +692,11 @@ static int8_t *PoWAVX_getPoWResult(void *pow_ctx)
return ret;
}

static void *PoWAVX_getPoWInfo(void *pow_ctx)
{
return ((PoW_AVX_Context *) pow_ctx)->pow_info;
}

ImplContext PoWAVX_Context = {
.context = NULL,
.description = "CPU (Intel AVX)",
Expand All @@ -693,4 +709,5 @@ ImplContext PoWAVX_Context = {
.freePoWContext = PoWAVX_freePoWContext,
.doThePoW = PowAVX,
.getPoWResult = PoWAVX_getPoWResult,
.getPoWInfo = PoWAVX_getPoWInfo,
};
3 changes: 3 additions & 0 deletions src/pow_avx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <pthread.h>
#include "common.h"
#include "constants.h"

typedef struct _pwork_struct Pwork_struct;
Expand Down Expand Up @@ -35,6 +36,8 @@ struct _pow_avx_context {
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;
};

bool PowAVX(void *pow_ctx);
Expand Down
Loading

0 comments on commit 8d95ec5

Please sign in to comment.