Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate and display hash count of PoW #73

Merged
merged 6 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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);
}

uint64_t getHashCount(ImplContext *impl_ctx, void *pow_ctx)
{
return impl_ctx->getHashCount(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);
uint64_t (*getHashCount)(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);
uint64_t getHashCount(ImplContext *impl_ctx, void *pow_ctx);

#endif
8 changes: 8 additions & 0 deletions src/pow_avx.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ bool PowAVX(void *pow_ctx)
/* Initialize the context */
PoW_AVX_Context *ctx = (PoW_AVX_Context *) pow_ctx;
ctx->stopPoW = 0;
ctx->hash_count = 0;
pthread_mutex_init(&ctx->lock, NULL);
pthread_t *threads = ctx->threads;
Pwork_struct *pitem = ctx->pitem;
Expand Down Expand Up @@ -573,6 +574,7 @@ bool PowAVX(void *pow_ctx)
pthread_join(threads[i], NULL);
if (pitem[i].n == -1)
completedIndex = i;
ctx->hash_count += (uint64_t) (pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1);
}

nonce_trit = initTrits(nonce_array[completedIndex], NonceTrinarySize);
Expand Down Expand Up @@ -681,6 +683,11 @@ static int8_t *PoWAVX_getPoWResult(void *pow_ctx)
return ret;
}

static uint64_t PoWAVX_getHashCount(void *pow_ctx)
{
return ((PoW_AVX_Context *) pow_ctx)->hash_count;
}

ImplContext PoWAVX_Context = {
.context = NULL,
.description = "CPU (Intel AVX)",
Expand All @@ -693,4 +700,5 @@ ImplContext PoWAVX_Context = {
.freePoWContext = PoWAVX_freePoWContext,
.doThePoW = PowAVX,
.getPoWResult = PoWAVX_getPoWResult,
.getHashCount = PoWAVX_getHashCount,
};
2 changes: 2 additions & 0 deletions src/pow_avx.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 result */
uint64_t hash_count;
};

bool PowAVX(void *pow_ctx);
Expand Down
8 changes: 8 additions & 0 deletions src/pow_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ bool PowC(void *pow_ctx)
/* Initialize the context */
PoW_C_Context *ctx = (PoW_C_Context *) pow_ctx;
ctx->stopPoW = 0;
ctx->hash_count = 0;
pthread_mutex_init(&ctx->lock, NULL);
pthread_t *threads = ctx->threads;
Pwork_struct *pitem = ctx->pitem;
Expand Down Expand Up @@ -334,6 +335,7 @@ bool PowC(void *pow_ctx)
pthread_join(threads[i], NULL);
if (pitem[i].n == -1)
completedIndex = i;
ctx->hash_count += (uint64_t) (pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1);
}

nonce_trit = initTrits(nonce_array[completedIndex], NonceTrinarySize);
Expand Down Expand Up @@ -441,6 +443,11 @@ static int8_t *PoWC_getPoWResult(void *pow_ctx)
return ret;
}

static uint64_t PoWC_getHashCount(void *pow_ctx)
{
return ((PoW_C_Context *) pow_ctx)->hash_count;
}

ImplContext PoWC_Context = {
.context = NULL,
.description = "CPU (Pure C)",
Expand All @@ -453,4 +460,5 @@ ImplContext PoWC_Context = {
.freePoWContext = PoWC_freePoWContext,
.doThePoW = PowC,
.getPoWResult = PoWC_getPoWResult,
.getHashCount = PoWC_getHashCount,
};
2 changes: 2 additions & 0 deletions src/pow_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct _pow_c_context {
int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int mwm;
/* PoW-related result */
uint64_t hash_count;
};

bool PowC(void *pow_ctx);
Expand Down
12 changes: 11 additions & 1 deletion src/pow_cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx)
char found = 0;
cl_event ev, ev1;
CLContext *titan = ctx;
ctx->hash_count = 0;
global_offset = 0;
num_groups = titan->num_cores;
local_work_size = STATE_TRITS_LENGTH;
Expand All @@ -99,14 +100,15 @@ static int8_t *pwork(int8_t *state, int mwm, CLContext *ctx)
int64_t mid_low[STATE_TRITS_LENGTH] = {0}, mid_high[STATE_TRITS_LENGTH] = {0};
init_state(state, mid_low, mid_high, HASH_TRITS_LENGTH - NONCE_TRITS_LENGTH);

if (!write_cl_buffer(titan, mid_low, mid_high, mwm, 32))
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)) {
clWaitForEvents(1, &ev);
clReleaseEvent(ev);
ctx->hash_count += 64 * num_groups * LOOP_COUNT;

while (found == 0) {
if (CL_SUCCESS !=
Expand Down Expand Up @@ -228,6 +230,8 @@ bool PowCL(void *pow_ctx)
}
memcpy(ctx->output_trytes, res_tryte->data, TRANSACTION_TRYTES_LENGTH);

ctx->hash_count = ctx->clctx->hash_count;

fail:
freeTrobject(tx_trit);
freeTrobject(tx_tryte);
Expand Down Expand Up @@ -290,6 +294,11 @@ static int8_t *PoWCL_getPoWResult(void *pow_ctx)
return ret;
}

static uint64_t PoWCL_getHashCount(void *pow_ctx)
{
return ((PoW_CL_Context *) pow_ctx)->hash_count;
}

ImplContext PoWCL_Context = {
.context = NULL,
.description = "GPU (OpenCL)",
Expand All @@ -302,4 +311,5 @@ ImplContext PoWCL_Context = {
.freePoWContext = PoWCL_freePoWContext,
.doThePoW = PowCL,
.getPoWResult = PoWCL_getPoWResult,
.getHashCount = PoWCL_getHashCount,
};
4 changes: 4 additions & 0 deletions src/pow_cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct _pow_cl_context {
int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int mwm;
/* PoW-related result */
uint64_t hash_count;
};

bool PowCL(void *pow_ctx);
Expand All @@ -34,4 +36,6 @@ bool PowCL(void *pow_ctx);
#define LOW_3 0xFFC0000007FFFFFF
#define HIGH_3 0x003FFFFFFFFFFFFF

#define LOOP_COUNT 32

#endif
7 changes: 7 additions & 0 deletions src/pow_fpga_accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
static bool PoWFPGAAccel(void *pow_ctx)
{
PoW_FPGA_Accel_Context *ctx = (PoW_FPGA_Accel_Context *) pow_ctx;
ctx->hash_count = 0;

int8_t fpga_out_nonce_trit[NonceTrinarySize];

Expand Down Expand Up @@ -173,6 +174,11 @@ static int8_t *PoWFPGAAccel_getPoWResult(void *pow_ctx)
return ret;
}

static uint64_t PoWFPGAAccel_getHashCount(void *pow_ctx)
{
return ((PoW_FPGA_Accel_Context *) pow_ctx)->hash_count;
}

ImplContext PoWFPGAAccel_Context = {
.context = NULL,
.description = "FPGA",
Expand All @@ -185,4 +191,5 @@ ImplContext PoWFPGAAccel_Context = {
.freePoWContext = PoWFPGAAccel_freePoWContext,
.doThePoW = PoWFPGAAccel,
.getPoWResult = PoWFPGAAccel_getPoWResult,
.getHashCount = PoWFPGAAccel_getHashCount,
};
2 changes: 2 additions & 0 deletions src/pow_fpga_accel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct _pow_fpga_accel_context {
int8_t input_trytes[(transactionTrinarySize) / 3]; /* 2673 */
marktwtn marked this conversation as resolved.
Show resolved Hide resolved
int8_t output_trytes[(transactionTrinarySize) / 3]; /* 2673 */
int mwm;
/* PoW-related result */
uint64_t hash_count;
/* Device files for the PFGA accelerator*/
int ctrl_fd;
int in_fd;
Expand Down
9 changes: 9 additions & 0 deletions src/pow_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "pow_sse.h"
#include <inttypes.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -318,6 +319,7 @@ bool PowSSE(void *pow_ctx)
/* Initialize the context */
PoW_SSE_Context *ctx = (PoW_SSE_Context *) pow_ctx;
ctx->stopPoW = 0;
ctx->hash_count = 0;
pthread_mutex_init(&ctx->lock, NULL);
pthread_t *threads = ctx->threads;
Pwork_struct *pitem = ctx->pitem;
Expand Down Expand Up @@ -350,6 +352,7 @@ bool PowSSE(void *pow_ctx)
pthread_join(threads[i], NULL);
if (pitem[i].n == -1)
completedIndex = i;
ctx->hash_count += (uint64_t) (pitem[i].ret >= 0 ? pitem[i].ret : -pitem[i].ret + 1);
}

nonce_trit = initTrits(nonce_array[completedIndex], NonceTrinarySize);
Expand Down Expand Up @@ -457,6 +460,11 @@ static int8_t *PoWSSE_getPoWResult(void *pow_ctx)
return ret;
}

static uint64_t PoWSSE_getHashCount(void *pow_ctx)
{
return ((PoW_SSE_Context *) pow_ctx)->hash_count;
}

ImplContext PoWSSE_Context = {
.context = NULL,
.description = "CPU (Intel SSE)",
Expand All @@ -469,4 +477,5 @@ ImplContext PoWSSE_Context = {
.freePoWContext = PoWSSE_freePoWContext,
.doThePoW = PowSSE,
.getPoWResult = PoWSSE_getPoWResult,
.getHashCount = PoWSSE_getHashCount,
};
2 changes: 2 additions & 0 deletions src/pow_sse.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct _pow_sse_context {
int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int mwm;
/* PoW-related result */
uint64_t hash_count;
};

bool PowSSE(void *pow_ctx);
Expand Down
3 changes: 2 additions & 1 deletion tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#include "pow_cl.h"
#endif

#include <stdlib.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef NDEBUG
#undef NDEBUG
Expand Down
3 changes: 3 additions & 0 deletions tests/test-pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ int main()
doThePoW(PoW_Context_ptr, pow_ctx);
int8_t *ret_trytes = getPoWResult(PoW_Context_ptr, pow_ctx);
assert(ret_trytes);
#if defined(ENABLE_STAT)
marktwtn marked this conversation as resolved.
Show resolved Hide resolved
printf("Hash count: %"PRIu64"\n", getHashCount(PoW_Context_ptr, pow_ctx));
#endif
freePoWContext(PoW_Context_ptr, pow_ctx);
destroyImplContext(PoW_Context_ptr);

Expand Down