Browse Source

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++?..
build-mingw
Noel Maersk 11 years ago
parent
commit
c6a27709f8
  1. 9
      algorithm.c
  2. 5
      algorithm.h
  3. 6
      miner.h
  4. 18
      ocl.c
  5. 24
      scrypt.c
  6. 5
      sgminer.c

9
algorithm.c

@ -17,12 +17,17 @@ void set_algorithm(algorithm_t* algo, const char* newname) { @@ -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;
}

5
algorithm.h

@ -7,8 +7,9 @@ @@ -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. */

6
miner.h

@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
#include "config.h"
#include "algorithm.h"
#include <stdbool.h>
#include <stdint.h>
#include <sys/time.h>
@ -1019,8 +1021,8 @@ extern int opt_queue; @@ -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;

18
ocl.c

@ -33,6 +33,12 @@ @@ -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) @@ -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) @@ -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) @@ -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: @@ -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: @@ -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

24
scrypt.c

@ -353,10 +353,12 @@ salsa20_8(uint32_t B[16], const uint32_t Bx[16]) @@ -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_ @@ -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_ @@ -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_ @@ -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) @@ -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 @@ -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 @@ -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)) {

5
sgminer.c

@ -97,7 +97,6 @@ int opt_expiry = 28; @@ -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() @@ -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) @@ -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;
}

Loading…
Cancel
Save