From c6a27709f8d2cc47af656baaa8b809cb54b0f16f Mon Sep 17 00:00:00 2001 From: Noel Maersk Date: Fri, 7 Mar 2014 01:29:55 +0200 Subject: [PATCH] core: use global algorithm structure instead of opt_nfactor. Also squashed: config: add log messages to set_algo() and set_nfactor(). algorithm: use set_algorithm_nfactor() when setting default nfactor in set_algorithm(). Otherwise algorithm->n defaults to 0. P.S. Did I already mention how this could have been C++?.. --- algorithm.c | 9 +++++++-- algorithm.h | 5 +++-- miner.h | 6 ++++-- ocl.c | 18 +++++++++++------- scrypt.c | 24 +++++++++++++----------- sgminer.c | 5 ++++- 6 files changed, 42 insertions(+), 25 deletions(-) diff --git a/algorithm.c b/algorithm.c index ba3d9cba..7399fe02 100644 --- a/algorithm.c +++ b/algorithm.c @@ -17,12 +17,17 @@ void set_algorithm(algorithm_t* algo, const char* newname) { algo->name[sizeof(algo->name) - 1] = '\0'; if (strcmp(algo->name, "adaptive-nfactor") == 0) { - algo->nfactor = 11; + set_algorithm_nfactor(algo, 11); } else { - algo->nfactor = 10; + set_algorithm_nfactor(algo, 10); } + + return; } void set_algorithm_nfactor(algorithm_t* algo, const uint8_t nfactor) { algo->nfactor = nfactor; + algo->n = (1 << nfactor); + + return; } diff --git a/algorithm.h b/algorithm.h index 0c73d9bb..b6053994 100644 --- a/algorithm.h +++ b/algorithm.h @@ -7,8 +7,9 @@ * a specific coin. */ typedef struct _algorithm_t { - char name[20]; /* Human-readable identifier */ - uint8_t nfactor; /* N factor (CPU/Memory tradeoff parameter) */ + char name[20]; /* Human-readable identifier */ + uint32_t n; /* N (CPU/Memory tradeoff parameter) */ + uint8_t nfactor; /* Factor of N above (n = 2^nfactor) */ } algorithm_t; /* Set default parameters based on name. */ diff --git a/miner.h b/miner.h index 81dea7a9..c932ab1b 100644 --- a/miner.h +++ b/miner.h @@ -3,6 +3,8 @@ #include "config.h" +#include "algorithm.h" + #include #include #include @@ -1019,8 +1021,8 @@ extern int opt_queue; extern int opt_scantime; extern int opt_expiry; -extern char* opt_algorithm; -extern int opt_nfactor; +extern char *opt_algorithm; +extern algorithm_t *algorithm; extern cglock_t control_lock; extern pthread_mutex_t hash_lock; diff --git a/ocl.c b/ocl.c index a456a6a7..68e6ed8b 100644 --- a/ocl.c +++ b/ocl.c @@ -33,6 +33,12 @@ #include "findnonce.h" #include "ocl.h" +/* FIXME: only here for global config vars, replace with configuration.h + * or similar as soon as config is in a struct instead of littered all + * over the global namespace. + */ +#include "miner.h" + int opt_platform_id = -1; char *file_contents(const char *filename, int *length) @@ -226,9 +232,6 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) cl_uint numDevices; cl_int status; - /* Scrypt CPU/Memory cost parameter */ - cl_uint N = (1 << opt_nfactor); - status = clGetPlatformIDs(0, NULL, &numPlatforms); if (status != CL_SUCCESS) { applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status); @@ -485,7 +488,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) if (!cgpu->opt_tc) { unsigned int sixtyfours; - sixtyfours = cgpu->max_alloc / 131072 / 64 / (N/1024) - 1; + sixtyfours = cgpu->max_alloc / 131072 / 64 / (algorithm->n/1024) - 1; cgpu->thread_concurrency = sixtyfours * 64; if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) { cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders; @@ -525,7 +528,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) if (clState->goffset) strcat(binaryfilename, "g"); - sprintf(numbuf, "lg%utc%unf%u", cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency,opt_nfactor); + sprintf(numbuf, "lg%utc%unf%u", cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, algorithm->nfactor); strcat(binaryfilename, numbuf); sprintf(numbuf, "w%d", (int)clState->wsize); @@ -592,7 +595,7 @@ build: char *CompilerOptions = (char *)calloc(1, 256); sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d -D NFACTOR=%d", - cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, (int)clState->wsize, (unsigned int) opt_nfactor); + cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, (int)clState->wsize, (unsigned int)algorithm->nfactor); applog(LOG_DEBUG, "Setting worksize to %d", (int)(clState->wsize)); if (clState->vwidth > 1) @@ -781,7 +784,8 @@ built: return NULL; } - size_t ipt = (N / cgpu->lookup_gap + (N % cgpu->lookup_gap > 0)); + size_t ipt = (algorithm->n / cgpu->lookup_gap + + (algorithm->n % cgpu->lookup_gap > 0)); size_t bufsize = 128 * ipt * cgpu->thread_concurrency; /* Use the max alloc value which has been rounded to a power of diff --git a/scrypt.c b/scrypt.c index 3b2e74de..3a48ea14 100644 --- a/scrypt.c +++ b/scrypt.c @@ -353,10 +353,12 @@ salsa20_8(uint32_t B[16], const uint32_t Bx[16]) B[15] += x15; } -/* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output - scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes +/* cpu and memory intensive function to transform a 80 byte buffer into + * a 32 byte output. + * scratchpad size needs to be at least (bytes): + * 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) */ -static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate, const cl_uint n) +static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate) { uint32_t * V; uint32_t X[32]; @@ -370,7 +372,7 @@ static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_ PBKDF2_SHA256_80_128(input, X); - for (i = 0; i < n; i += 2) { + for (i = 0; i < algorithm->n; i += 2) { memcpy(&V[i * 32], X, 128); salsa20_8(&X[0], &X[16]); @@ -381,8 +383,8 @@ static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_ salsa20_8(&X[0], &X[16]); salsa20_8(&X[16], &X[0]); } - for (i = 0; i < n; i += 2) { - j = X[16] & (n-1); + for (i = 0; i < algorithm->n; i += 2) { + j = X[16] & (algorithm->n-1); p2 = (uint64_t *)(&V[j * 32]); for(k = 0; k < 16; k++) p1[k] ^= p2[k]; @@ -390,7 +392,7 @@ static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_ salsa20_8(&X[0], &X[16]); salsa20_8(&X[16], &X[0]); - j = X[16] & (n-1); + j = X[16] & (algorithm->n-1); p2 = (uint64_t *)(&V[j * 32]); for(k = 0; k < 16; k++) p1[k] ^= p2[k]; @@ -412,8 +414,8 @@ void scrypt_regenhash(struct work *work) be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); - scratchbuf = (char *)alloca((1 << opt_nfactor) * 128 + 512); - scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << opt_nfactor)); + scratchbuf = (char *)alloca(algorithm->n * 128 + 512); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash); flip32(ohash, ohash); } @@ -431,7 +433,7 @@ int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t non be32enc_vect(data, (const uint32_t *)pdata, 19); data[19] = htobe32(nonce); scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); - scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << opt_nfactor)); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash, algorithm->n); tmp_hash7 = be32toh(ohash[7]); applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", @@ -470,7 +472,7 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p *nonce = ++n; data[19] = htobe32(n); - scrypt_n_1_1_256_sp(data, scratchbuf, ostate, (1 << opt_nfactor)); + scrypt_n_1_1_256_sp(data, scratchbuf, ostate, algorithm->n); tmp_hash7 = be32toh(ostate[7]); if (unlikely(tmp_hash7 <= Htarg)) { diff --git a/sgminer.c b/sgminer.c index aaeb3828..d759c45d 100644 --- a/sgminer.c +++ b/sgminer.c @@ -97,7 +97,6 @@ int opt_expiry = 28; char *opt_algorithm; algorithm_t *algorithm; -int opt_nfactor = 10; static const bool opt_time = true; unsigned long long global_hashrate; @@ -1014,6 +1013,7 @@ static void load_temp_cutoffs() static char *set_algo(const char *arg) { set_algorithm(algorithm, arg); + applog(LOG_INFO, "Set algorithm to %s", algorithm->name); return NULL; } @@ -1021,6 +1021,9 @@ static char *set_algo(const char *arg) static char *set_nfactor(const char *arg) { set_algorithm_nfactor(algorithm, (uint8_t)atoi(arg)); + applog(LOG_INFO, "Set algorithm N-factor to %d (N to %d)", + algorithm->nfactor, algorithm->n); + return NULL; }