Skip to content

Commit

Permalink
Merge branch 'task/change_prototype' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Wei, CHEN committed Mar 16, 2018
2 parents e45ea75 + d9d968d commit ca899ee
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 42 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ DISABLE_JNI ?= 1

CFLAGS = -Os -fPIC -g
LDFLAGS = -lpthread
PYFLAGS = --gpu_enable

# FIXME: avoid hardcoded architecture flags. We might support advanced SIMD
# instructions for Intel and Arm later.
Expand All @@ -17,6 +18,9 @@ CFLAGS += \
-DENABLE_OPENCL \
-I$(OPENCL_PATH)/include
LDFLAGS += -L$(OPENCL_LIB) -lOpenCL
PYFLAGS += 1
else
PYFLAGS += 0
endif

# FIXME: avoid hardcoded OS path
Expand Down Expand Up @@ -93,10 +97,14 @@ $(OUT)/libdcurl.so: $(OBJS)
$(VECHO) " LD\t$@\n"
$(Q)$(CC) -shared -o $@ $^ $(LDFLAGS)

test_multi_pow: test/test_multi_pow.py $(OUT)/libdcurl.so
$(Q)$(PRINTF) "*** Validating $< ***\n"
$(Q)python3 $< $(PYFLAGS) && $(PRINTF) "\t$(PASS_COLOR)[ Verified ]$(NO_COLOR)\n"

$(OUT)/test_%.done: $(OUT)/test_%
$(Q)$(PRINTF) "*** Validating $< ***\n"
$(Q)./$< && $(PRINTF) "\t$(PASS_COLOR)[ Verified ]$(NO_COLOR)\n"
check: $(addsuffix .done, $(TESTS))
check: $(addsuffix .done, $(TESTS)) test_multi_pow

clean:
$(RM) -r $(OUT)
Expand Down
17 changes: 10 additions & 7 deletions jni/iri-pearldiver-exlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <jni.h>
#include "../src/dcurl.h"
#include "../src/trinary/trinary.h"
#include <stdint.h>

static char *int_to_char_array(jint *arr, int size)
static int8_t *int_to_char_array(jint *arr, int size)
{
char *ret = malloc(size);
int8_t *ret = malloc(size);
if (!ret)
return NULL;

Expand All @@ -15,7 +16,7 @@ static char *int_to_char_array(jint *arr, int size)
return ret;
}

static jint *char_to_int_array(char *arr, int size)
static jint *char_to_int_array(int8_t *arr, int size)
{
jint *ret = malloc(sizeof(jint) * size);
if (!ret)
Expand Down Expand Up @@ -43,7 +44,7 @@ Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEnv *env,
{
/* Convert ***INT*** array (TRITS) to ***CHAR*** array (TRYTES) */
jint *c_trits = (*env)->GetIntArrayElements(env, trits, NULL);
char *char_trits = int_to_char_array(c_trits, 8019);
int8_t *char_trits = int_to_char_array(c_trits, 8019);
if (!char_trits)
return JNI_FALSE;

Expand All @@ -53,10 +54,11 @@ Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEnv *env,
return JNI_FALSE;
/****************************************************************/

Trytes_t *result = dcurl_entry(arg_trytes, mwm);
int8_t *result = dcurl_entry(arg_trytes->data, mwm);

/* Convert ***CHAR*** array(TRYTES) to ***INT*** array (TRITS) */
Trits_t *ret_trits = trits_from_trytes(result);
Trytes_t *ret_trytes = initTrytes(result, 2673);
Trits_t *ret_trits = trits_from_trytes(ret_trytes);
if (!ret_trits)
return JNI_FALSE;

Expand All @@ -70,7 +72,8 @@ Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEnv *env,
/* Free */
free(char_trits);
free(int_trits);
freeTrobject(result);
free(result);
freeTrobject(ret_trytes);
freeTrobject(arg_trits);
freeTrobject(arg_trytes);
freeTrobject(ret_trits);
Expand Down
4 changes: 2 additions & 2 deletions src/dcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void dcurl_destroy()
#endif
}

Trytes_t *dcurl_entry(Trytes_t *trytes, int mwm)
int8_t *dcurl_entry(int8_t *trytes, int mwm)
{
static int num_cpu_thread = 0;
static int num_gpu_thread = 0;
Expand Down Expand Up @@ -118,7 +118,7 @@ Trytes_t *dcurl_entry(Trytes_t *trytes, int mwm)
pthread_mutex_unlock(&mtx);
}

Trytes_t *ret_trytes = NULL;
int8_t *ret_trytes = NULL;

switch (selected_entry) {
case 1:
Expand Down
2 changes: 1 addition & 1 deletion src/dcurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

int dcurl_init(int max_cpu_thread, int max_gpu_thread);
void dcurl_destroy();
Trytes_t *dcurl_entry(Trytes_t *trytes, int mwm);
int8_t *dcurl_entry(int8_t *trytes, int mwm);

#endif
18 changes: 11 additions & 7 deletions src/pow_cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ static int8_t *pwork(int8_t *state, int mwm, int index)
int8_t *buf = malloc(HASH_LENGTH);

if (found > 0) {
if (CL_SUCCESS != clEnqueueReadBuffer(
titan->cmdq, titan->buffer[0], CL_TRUE, 0,
HASH_LENGTH * sizeof(int8_t), buf, 1, &ev, NULL)) {
if (CL_SUCCESS != clEnqueueReadBuffer(titan->cmdq, titan->buffer[0],
CL_TRUE, 0,
HASH_LENGTH * sizeof(int8_t), buf,
1, &ev, NULL)) {
return NULL; /* Read buffer failed */
}
}
Expand Down Expand Up @@ -243,10 +244,9 @@ static int8_t *tx_to_cstate(Trytes_t *tx)
return c_state;
}

Trytes_t *PowCL(Trytes_t *trytes, int mwm, int index)
int8_t *PowCL(int8_t *trytes, int mwm, int index)
{
// Trytes_t *trytes_t = initTrytes(trytes, 2673);
Trytes_t *trytes_t = trytes;
Trytes_t *trytes_t = initTrytes(trytes, 2673);

Trits_t *tr = trits_from_trytes(trytes_t);
if (!tr)
Expand All @@ -264,10 +264,14 @@ Trytes_t *PowCL(Trytes_t *trytes, int mwm, int index)
HASH_LENGTH * sizeof(int8_t));

Trytes_t *last = trytes_from_trits(tr);
int8_t *ret_data = last->data;

freeTrobject(tr);
freeTrobject(trytes_t);
/* hack */
free(last);
free(c_state);
free(ret);

return last;
return ret_data;
}
2 changes: 1 addition & 1 deletion src/pow_cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "./trinary/trinary.h"

Trytes_t *PowCL(Trytes_t *trytes, int mwm, int index);
int8_t *PowCL(int8_t *trytes, int mwm, int index);
int pwork_ctx_init(int context_size);
void pwork_ctx_destroy(int context_size);

Expand Down
20 changes: 11 additions & 9 deletions src/pow_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ static void incrN128(int n, __m128i *mid_low, __m128i *mid_high)
}
}

static long long int pwork128(int8_t mid[], int mwm, int8_t nonce[], int n, int id)
static long long int pwork128(int8_t mid[],
int mwm,
int8_t nonce[],
int n,
int id)
{
__m128i lmid[STATE_LENGTH], hmid[STATE_LENGTH];
para128(mid, lmid, hmid);
Expand Down Expand Up @@ -324,7 +328,7 @@ static int8_t *tx_to_cstate(Trytes_t *tx)
return c_state;
}

static Trytes_t *nonce_to_result(Trytes_t *tx, Trytes_t *nonce)
static int8_t *nonce_to_result(Trytes_t *tx, Trytes_t *nonce)
{
int rst_len = tx->len - NonceTrinarySize / 3 + nonce->len;
int8_t *rst = (int8_t *) malloc(rst_len);
Expand All @@ -335,10 +339,7 @@ static Trytes_t *nonce_to_result(Trytes_t *tx, Trytes_t *nonce)
memcpy(rst + tx->len - NonceTrinarySize / 3, nonce->data,
rst_len - (tx->len - NonceTrinarySize / 3));

Trytes_t *ret = initTrytes(rst, rst_len);
free(rst);

return ret;
return rst;
}

int pow_sse_init(int num_task)
Expand Down Expand Up @@ -366,12 +367,12 @@ void pow_sse_destroy()
free(countSSE);
}

Trytes_t *PowSSE(Trytes_t *trytes, int mwm, int index)
int8_t *PowSSE(int8_t *trytes, int mwm, int index)
{
stopSSE[index] = 0;
countSSE[index] = 0;

Trytes_t *trytes_t = trytes;
Trytes_t *trytes_t = initTrytes(trytes, 2673);

int8_t *c_state = tx_to_cstate(trytes_t);
if (!c_state)
Expand Down Expand Up @@ -421,7 +422,7 @@ Trytes_t *PowSSE(Trytes_t *trytes, int mwm, int index)
if (!nonce)
return NULL;

Trytes_t *last_result = nonce_to_result(trytes_t, nonce);
int8_t *last_result = nonce_to_result(trytes_t, nonce);

/* Free memory */
free(c_state);
Expand All @@ -431,6 +432,7 @@ Trytes_t *PowSSE(Trytes_t *trytes, int mwm, int index)
free(nonce_array);
free(threads);
free(pitem);
freeTrobject(trytes_t);
freeTrobject(nonce_t);
freeTrobject(nonce);

Expand Down
2 changes: 1 addition & 1 deletion src/pow_sse.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct _pwork_struct {
long long int ret;
};

Trytes_t *PowSSE(Trytes_t *trytes, int mwm, int index);
int8_t *PowSSE(int8_t *trytes, int mwm, int index);
int pow_sse_init(int num_task);
void pow_sse_destroy();

Expand Down
1 change: 1 addition & 0 deletions test/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#endif

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

#ifdef NDEBUG
#undef NDEBUG
Expand Down
92 changes: 92 additions & 0 deletions test/test_multi_pow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-

import ctypes
import sys
import argparse
import _thread
import iota
from iota import TryteString
from iota.crypto import Curl

TRYTES_LIST_PATH = "./test/trytes.txt"
DCURL_PATH = "./build/libdcurl.so"
NUM_TRYTES = 10

join_list = []
for i in range(NUM_TRYTES):
join_list.append(_thread.allocate_lock())

# return list of trytes #
def read_trytes(FILE_PATH):
f = open(FILE_PATH, 'r')
row = f.readlines()
tmp = []
for r in row:
tmp.append(r.split('\n')[0])
return tmp

def hash(trytes):
curl = Curl()
curl.absorb(trytes.as_trits())
trits_out = []
curl.squeeze(trits_out)
return TryteString.from_trits(trits_out)

def validate(trytes, mwm):
trits = trytes.as_trits()
for i in range(len(trits) - 1, len(trits) - mwm - 1, -1):
if trits[i] != 0:
return False
return True

def call_dcurl(idx, mwm, lib, trytes_list):
tmp = str(trytes_list[idx]).encode('ascii')
ret = lib.dcurl_entry(tmp, mwm)
trytes = TryteString(ret)

hash_trytes = hash(trytes)
if validate(hash_trytes, mwm) is not True:
sys.exit(1)

join_list[idx].release()

def testing(dcurl_parameter):
num_cpu = dcurl_parameter[0]
num_gpu = dcurl_parameter[1]
trytes_list = read_trytes(TRYTES_LIST_PATH)

# Settings of shared library
libdcurl = ctypes.cdll.LoadLibrary(DCURL_PATH)
libdcurl.dcurl_init.argtypes = [ctypes.c_int, ctypes.c_int]
libdcurl.dcurl_entry.argtypes = [ctypes.c_char_p, ctypes.c_int]
libdcurl.dcurl_entry.restype = ctypes.c_char_p

libdcurl.dcurl_init(num_cpu, num_gpu)

for i in range(NUM_TRYTES):
join_list[i].acquire()
_thread.start_new_thread(call_dcurl, (i, 14, libdcurl, trytes_list, ))

# threadpool.join()
for i in range(NUM_TRYTES):
while join_list[i].locked(): pass

libdcurl.dcurl_destroy()

if __name__ == "__main__":
# Parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument("--gpu_enable", type = int);
args = parser.parse_args()

# Select testing set, (x, y) which means
# (MAX_CPU_THREAD, MAX_GPU_THREAD) in dcurl
test_set = (0, 0)
if args.gpu_enable == 0:
test_set = (2, 0)
else:
test_set = (2, 1)

testing(test_set)

sys.exit(0)
14 changes: 7 additions & 7 deletions test/test_pow_cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

int main()
{
char *txt =
char *trytes =
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
Expand Down Expand Up @@ -44,26 +44,26 @@ int main()
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999";

Trytes_t *trytes = initTrytes((signed char *) txt, 2673);
int mwm = 14;

/* test OpenCL Implementation with mwm = 14 */
pwork_ctx_init(1);
Trytes_t *ret_trytes = PowCL(trytes, mwm, 0);
int8_t *ret_trytes = PowCL(trytes, mwm, 0);
pwork_ctx_destroy(1);

Trytes_t *hash_trytes = hashTrytes(ret_trytes);
Trytes_t *trytes_t = initTrytes(ret_trytes, 2673);
Trytes_t *hash_trytes = hashTrytes(trytes_t);

/* Validation */
Trits_t *ret_trits = trits_from_trytes(hash_trytes);
for (int i = 243 - 1; i >= 243 - mwm; i--) {
assert(ret_trits->data[i] == 0);
}

freeTrobject(trytes);
freeTrobject(ret_trytes);
free(ret_trytes);
freeTrobject(trytes_t);
freeTrobject(hash_trytes);
freeTrobject(ret_trits);

return 0;
}
}
Loading

0 comments on commit ca899ee

Please sign in to comment.