Skip to content

Commit

Permalink
Merge pull request #7 from jserv/indent-clang-format
Browse files Browse the repository at this point in the history
Indent with clang-format
  • Loading branch information
WEI, CHEN committed Mar 16, 2018
2 parents 0aa7a95 + fcc66bb commit 0a04574
Show file tree
Hide file tree
Showing 16 changed files with 1,673 additions and 1,327 deletions.
15 changes: 15 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BasedOnStyle: Chromium
Language: Cpp
MaxEmptyLinesToKeep: 3
IndentCaseLabels: false
AllowShortIfStatementsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
DerivePointerAlignment: false
PointerAlignment: Right
SpaceAfterCStyleCast: true
TabWidth: 4
UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Linux
AccessModifierOffset: -4
29 changes: 18 additions & 11 deletions jni/iri-pearldiver-exlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <jni.h>
#include "iri-pearldiver-exlib.h"
#include <jni.h>
#include "../src/dcurl.h"
#include "../src/trinary/trinary.h"

Expand All @@ -23,29 +23,34 @@ static jint *char_to_int_array(char *arr, int size)
return ret;
}

JNIEXPORT jboolean JNICALL Java_com_iota_iri_hash_PearlDiver_exlib_1init(JNIEnv *env, jclass clazz)
JNIEXPORT jboolean JNICALL
Java_com_iota_iri_hash_PearlDiver_exlib_1init(JNIEnv *env, jclass clazz)
{
dcurl_init(2, 1);
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEnv *env, jclass clazz, jintArray trits, jint mwm)
JNIEXPORT jboolean JNICALL
Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEnv *env,
jclass clazz,
jintArray trits,
jint mwm)
{
/* Convert ***INT*** array (TRITS) to ***CHAR*** array (TRYTES) */
/* 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);

Trits_t *arg_trits = initTrits(char_trits, 8019);
Trytes_t *arg_trytes = trytes_from_trits(arg_trits);
/****************************************************************/

Trytes_t *result = dcurl_entry(arg_trytes, mwm);

/* Convert ***CHAR*** array(TRYTES) to ***INT*** array (TRITS) */
Trits_t *ret_trits = trits_from_trytes(result);
jint *int_trits = char_to_int_array(ret_trits->data, 8019);
/***************************************************************/

/*
jintArray returnJNIArray = (*env)->NewIntArray(env, 8019);
(*env)->SetIntArrayRegion(env, returnJNIArray, 0, 8019, int_trits);
Expand All @@ -60,16 +65,18 @@ JNIEXPORT jboolean JNICALL Java_com_iota_iri_hash_PearlDiver_exlib_1search(JNIEn
freeTrobject(arg_trits);
freeTrobject(arg_trytes);
freeTrobject(ret_trits);

return JNI_TRUE;
}

JNIEXPORT void JNICALL Java_com_iota_iri_hash_PearlDiver_exlib_1cancel(JNIEnv *env, jclass clazz)
JNIEXPORT void JNICALL
Java_com_iota_iri_hash_PearlDiver_exlib_1cancel(JNIEnv *env, jclass clazz)
{
/* Do nothing */
}

JNIEXPORT void JNICALL Java_com_iota_iri_hash_PearlDiver_exlib_1destroy(JNIEnv *env, jclass clazz)
JNIEXPORT void JNICALL
Java_com_iota_iri_hash_PearlDiver_exlib_1destroy(JNIEnv *env, jclass clazz)
{
dcurl_destroy();
}
19 changes: 11 additions & 8 deletions jni/iri-pearldiver-exlib.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 33 additions & 19 deletions src/clcontext.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "clcontext.h"
#include "pearl.cl.h"
#include <stdio.h>
#include "pearl.cl.h"

void init_cl_devices(CLContext *ctx)
{
Expand All @@ -9,7 +9,7 @@ void init_cl_devices(CLContext *ctx)
cl_platform_id *platform;

errno = clGetPlatformIDs(0, NULL, &num_platform);

if (errno != CL_SUCCESS) {
printf("Cannot get the number of OpenCL platforms available.\n");
exit(0);
Expand All @@ -18,35 +18,42 @@ void init_cl_devices(CLContext *ctx)
// We only need one Platform
platform = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platform);
clGetPlatformIDs(num_platform, platform, NULL);

// Get Device IDs
cl_uint platform_num_device;
if (clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_GPU, 1, &ctx->device, &platform_num_device) != CL_SUCCESS) {
if (clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_GPU, 1, &ctx->device,
&platform_num_device) != CL_SUCCESS) {
printf("Failed to get OpenCL Device IDs for platform.\n");
exit(0);
}

// Create OpenCL context
ctx->context = (cl_context) clCreateContext(NULL, 1, &ctx->device, NULL, NULL, &errno);
ctx->context =
(cl_context) clCreateContext(NULL, 1, &ctx->device, NULL, NULL, &errno);
if (errno != CL_SUCCESS) {
printf("Failed to create OpenCL Context\n");
exit(0);
}

// Get Device Info (num_cores)
if (CL_SUCCESS != clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &ctx->num_cores, NULL)) {
if (CL_SUCCESS != clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(cl_uint), &ctx->num_cores, NULL)) {
printf("Failed to get Device info (num_cores)!\n");
exit(0);
}

// Get Device Info (max_memory)
if (CL_SUCCESS != clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &ctx->max_memory, NULL)) {
if (CL_SUCCESS != clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_MEM_ALLOC_SIZE,
sizeof(cl_ulong), &ctx->max_memory,
NULL)) {
printf("Failed to get Device info (max memory)!\n");
exit(0);
}

// Get Device Info (num work group)
if (CL_SUCCESS != clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &ctx->num_work_group, NULL)) {
if (CL_SUCCESS !=
clGetDeviceInfo(ctx->device, CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t), &ctx->num_work_group, NULL)) {
ctx->num_work_group = 1;
}

Expand All @@ -67,13 +74,16 @@ void init_cl_program(CLContext *ctx)
size_t source_size = pearl_cl_len;
cl_int errno;

ctx->program = clCreateProgramWithSource(ctx->context, ctx->kernel_info.num_src, (const char **) &source_str, (const size_t *) &source_size, &errno);
ctx->program = clCreateProgramWithSource(
ctx->context, ctx->kernel_info.num_src, (const char **) &source_str,
(const size_t *) &source_size, &errno);
if (CL_SUCCESS != errno) {
printf("Failed to create program with source\n");
exit(0);
}

errno = clBuildProgram(ctx->program, 1, &ctx->device, "-Werror", NULL, NULL);
errno =
clBuildProgram(ctx->program, 1, &ctx->device, "-Werror", NULL, NULL);
if (CL_SUCCESS != errno) {
printf("Failed to build program\n");
exit(0);
Expand All @@ -86,7 +96,7 @@ void init_cl_kernel(CLContext *ctx, char **kernel_name)

for (int i = 0; i < ctx->kernel_info.num_kernels; i++) {
ctx->kernel[i] = clCreateKernel(ctx->program, kernel_name[i], &errno);
if(CL_SUCCESS != errno) {
if (CL_SUCCESS != errno) {
printf("Failed to create kernel\n");
exit(0);
}
Expand All @@ -103,27 +113,31 @@ void init_cl_buffer(CLContext *ctx)
if (ctx->kernel_info.buffer_info[i].init_flags & 2) {
mem *= ctx->num_cores * ctx->num_work_group;
if (mem > ctx->max_memory) {
int temp = ctx->max_memory / ctx->kernel_info.buffer_info[i].size;
int temp =
ctx->max_memory / ctx->kernel_info.buffer_info[i].size;
ctx->num_cores = temp;
mem = temp * ctx->kernel_info.buffer_info[i].size;
}
}
// Check Memory bound
// Check Memory bound
max_mem += mem;
if (max_mem >= ctx->max_memory) {
printf("Max memory pass\n");
exit(0);
}

// Create OpenCL Buffer
ctx->buffer[i] = clCreateBuffer(ctx->context, ctx->kernel_info.buffer_info[i].flags, mem, NULL, &errno);
ctx->buffer[i] =
clCreateBuffer(ctx->context, ctx->kernel_info.buffer_info[i].flags,
mem, NULL, &errno);
if (CL_SUCCESS != errno) {
printf("Failed to create Buffer\n");
exit(0);
}
// Set Kernel Arguments
for (int j = 0; j < ctx->kernel_info.num_kernels; j++) {
if (CL_SUCCESS != clSetKernelArg(ctx->kernel[j], i, sizeof(cl_mem), (void *) &ctx->buffer[i])) {
if (CL_SUCCESS != clSetKernelArg(ctx->kernel[j], i, sizeof(cl_mem),
(void *) &ctx->buffer[i])) {
printf("Failed to set kernel argements\n");
exit(0);
}
Expand All @@ -135,15 +149,15 @@ int init_clcontext(CLContext **ctx)
{
*ctx = (CLContext *) malloc(sizeof(CLContext));

if(!(*ctx)) {
if (!(*ctx)) {
printf("clcontext.c: init_clcontext: unavailable malloc\n");
exit(0);
}

(*ctx)->kernel_info.num_buffers = 9;
(*ctx)->kernel_info.num_kernels = 3;
(*ctx)->kernel_info.num_src = 1;

init_cl_devices(*ctx);
init_cl_program(*ctx);

Expand Down
32 changes: 24 additions & 8 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#define SignatureMessageFragmentTrinaryOffset 0
#define SignatureMessageFragmentTrinarySize 6561
#define AddressTrinaryOffset SignatureMessageFragmentTrinaryOffset + SignatureMessageFragmentTrinarySize
#define AddressTrinaryOffset \
SignatureMessageFragmentTrinaryOffset + SignatureMessageFragmentTrinarySize
#define AddressTrinarySize 243
#define ValueTrinaryOffset AddressTrinaryOffset + AddressTrinarySize
#define ValueTrinarySize 81
Expand All @@ -20,27 +21,42 @@
#define TimestampTrinarySize 27
#define CurrentIndexTrinaryOffset TimestampTrinaryOffset + TimestampTrinarySize
#define CurrentIndexTrinarySize 27
#define LastIndexTrinaryOffset CurrentIndexTrinaryOffset + CurrentIndexTrinarySize
#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 BranchTransactionTrinaryOffset \
TrunkTransactionTrinaryOffset + TrunkTransactionTrinarySize
#define BranchTransactionTrinarySize 243
#define TagTrinaryOffset BranchTransactionTrinaryOffset + BranchTransactionTrinarySize
#define TagTrinaryOffset \
BranchTransactionTrinaryOffset + BranchTransactionTrinarySize
#define TagTrinarySize 81
#define AttachmentTimestampTrinaryOffset TagTrinaryOffset + TagTrinarySize
#define AttachmentTimestampTrinarySize 27

#define AttachmentTimestampLowerBoundTrinaryOffset AttachmentTimestampTrinaryOffset + AttachmentTimestampTrinarySize
#define AttachmentTimestampLowerBoundTrinaryOffset \
AttachmentTimestampTrinaryOffset + AttachmentTimestampTrinarySize
#define AttachmentTimestampLowerBoundTrinarySize 27
#define AttachmentTimestampUpperBoundTrinaryOffset AttachmentTimestampLowerBoundTrinaryOffset + AttachmentTimestampLowerBoundTrinarySize
#define AttachmentTimestampUpperBoundTrinaryOffset \
AttachmentTimestampLowerBoundTrinaryOffset + \
AttachmentTimestampLowerBoundTrinarySize
#define AttachmentTimestampUpperBoundTrinarySize 27
#define NonceTrinaryOffset AttachmentTimestampUpperBoundTrinaryOffset + AttachmentTimestampUpperBoundTrinarySize
#define NonceTrinaryOffset \
AttachmentTimestampUpperBoundTrinaryOffset + \
AttachmentTimestampUpperBoundTrinarySize
#define NonceTrinarySize 81

#define transactionTrinarySize SignatureMessageFragmentTrinarySize + AddressTrinarySize + ValueTrinarySize + ObsoleteTagTrinarySize + TimestampTrinarySize + CurrentIndexTrinarySize + LastIndexTrinarySize + BundleTrinarySize + TrunkTransactionTrinarySize + BranchTransactionTrinarySize + TagTrinarySize + AttachmentTimestampTrinarySize + AttachmentTimestampLowerBoundTrinarySize + AttachmentTimestampUpperBoundTrinarySize + NonceTrinarySize
#define transactionTrinarySize \
SignatureMessageFragmentTrinarySize + AddressTrinarySize + \
ValueTrinarySize + ObsoleteTagTrinarySize + TimestampTrinarySize + \
CurrentIndexTrinarySize + LastIndexTrinarySize + BundleTrinarySize + \
TrunkTransactionTrinarySize + BranchTransactionTrinarySize + \
TagTrinarySize + AttachmentTimestampTrinarySize + \
AttachmentTimestampLowerBoundTrinarySize + \
AttachmentTimestampUpperBoundTrinarySize + NonceTrinarySize

extern char TryteAlphabet[];

Expand Down
Loading

0 comments on commit 0a04574

Please sign in to comment.