Skip to content

Commit

Permalink
Code refurbishment
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanLucPons committed Jun 17, 2020
1 parent 5fde51c commit 3727236
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ uint32_t Kangaroo::CheckHash(uint32_t h,uint32_t nbItem,HashTable* hT,FILE* f) {
e = hT->E[h].items[i];
Int dist;
uint32_t kType;
HashTable::CalcCollision(e->d,&dist,&kType);
HashTable::CalcDistAndType(e->d,&dist,&kType);
dists.push_back(dist);
types.push_back(kType);
}
Expand All @@ -61,7 +61,7 @@ uint32_t Kangaroo::CheckHash(uint32_t h,uint32_t nbItem,HashTable* hT,FILE* f) {
e = items + i;
Int dist;
uint32_t kType;
HashTable::CalcCollision(e->d,&dist,&kType);
HashTable::CalcDistAndType(e->d,&dist,&kType);
dists.push_back(dist);
types.push_back(kType);
}
Expand Down
2 changes: 1 addition & 1 deletion Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define CONSTANTSH

// Release number
#define RELEASE "1.11gamma"
#define RELEASE "1.11theta"

// Use symmetry
//#define USE_SYMMETRY
Expand Down
8 changes: 4 additions & 4 deletions HashTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ int HashTable::MergeH(uint32_t h,FILE* f1,FILE* f2,FILE* fd,uint32_t* nbDP,uint3
*duplicate = *duplicate + 1;
} else {
// Collision
CalcCollision(e1.d,d1,k1);
CalcCollision(e2.d,d2,k2);
CalcDistAndType(e1.d,d1,k1);
CalcDistAndType(e2.d,d2,k2);
collisionFound = true;
}
memcpy(output + nbd,&e1,32);
Expand Down Expand Up @@ -246,7 +246,7 @@ int HashTable::Add(uint64_t h,int128_t *x,int128_t *d) {

}

void HashTable::CalcCollision(int128_t d,Int* kDist,uint32_t* kType) {
void HashTable::CalcDistAndType(int128_t d,Int* kDist,uint32_t* kType) {

*kType = (d.i64[1] & 0x4000000000000000ULL) != 0;
int sign = (d.i64[1] & 0x8000000000000000ULL) != 0;
Expand Down Expand Up @@ -293,7 +293,7 @@ int HashTable::Add(uint64_t h,ENTRY* e) {
}

// Collision
CalcCollision(GET(h,mi)->d , &kDist, &kType);
CalcDistAndType(GET(h,mi)->d , &kDist, &kType);
return ADD_COLLISION;

} else {
Expand Down
2 changes: 1 addition & 1 deletion HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class HashTable {
static void Convert(Int *x,Int *d,uint32_t type,uint64_t *h,int128_t *X,int128_t *D);
static int MergeH(uint32_t h,FILE* f1,FILE* f2,FILE* fd,uint32_t *nbDP,uint32_t* duplicate,
Int* d1,uint32_t* k1,Int* d2,uint32_t* k2);
static void CalcCollision(int128_t d,Int* kDist,uint32_t* kType);
static void CalcDistAndType(int128_t d,Int* kDist,uint32_t* kType);

private:

Expand Down
29 changes: 12 additions & 17 deletions Kangaroo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Kangaroo::Kangaroo(Secp256K1 *secp,int32_t initDPSize,bool useGpu,string &workFi
this->collisionInSameHerd = 0;
this->keyIdx = 0;
this->splitWorkfile = splitWorkfile;
this->pid = Timer::getPID();

CPU_GRP_SIZE = 1024;

Expand Down Expand Up @@ -318,7 +319,7 @@ bool Kangaroo::AddToTable(uint64_t h,int128_t *x,int128_t *d) {

Int dist;
uint32_t kType;
HashTable::CalcCollision(*d,&dist,&kType);
HashTable::CalcDistAndType(*d,&dist,&kType);
return CollisionCheck(&hashTable.kDist,hashTable.kType,&dist,kType);

}
Expand Down Expand Up @@ -446,7 +447,7 @@ void Kangaroo::SolveKeyCPU(TH_PARAM *ph) {
double now = Timer::get_tick();
if( now-lastSent > SEND_PERIOD ) {
LOCK(ghMutex);
SendToServer(dps);
SendToServer(dps,ph->threadId,0xFFFF);
UNLOCK(ghMutex);
lastSent = now;
}
Expand Down Expand Up @@ -575,7 +576,7 @@ void Kangaroo::SolveKeyGPU(TH_PARAM *ph) {
double now = Timer::get_tick();
if(now - lastSent > SEND_PERIOD) {
LOCK(ghMutex);
SendToServer(dps);
SendToServer(dps,ph->threadId,ph->gpuId);
UNLOCK(ghMutex);
lastSent = now;
}
Expand Down Expand Up @@ -845,20 +846,14 @@ void Kangaroo::ComputeExpected(double dp,double *op,double *ram,double *overHead
// theta
double theta = pow(2.0,dp);

// Average for DP = 0
double avgDP0 = (2.0 * (2.0 - sqrt(2.0)) * gainS) * sqrt(M_PI) * sqrt(N);

// DP Overhead (for small number of kangaroo)
double overSmallK = k * theta;
// Z0
double Z0 = (2.0 * (2.0 - sqrt(2.0)) * gainS) * sqrt(M_PI);

// Average estimation (when k >> theta)
double overLargeK = pow(16.0 * N * theta * k,1.0 / 3.0);
// Average for DP = 0
double avgDP0 = Z0 * sqrt(N);

double avg = avgDP0 + overSmallK;
if(overLargeK>avg)
*op = overLargeK;
else
*op = avg;
// DP Overhead
*op = Z0 * pow(N * (k * theta + sqrt(N)),1.0 / 3.0);

*ram = (double)sizeof(HASH_ENTRY) * (double)HASH_SIZE + // Table
(double)sizeof(ENTRY *) * (double)(HASH_SIZE * 4) + // Allocation overhead
Expand Down Expand Up @@ -1001,7 +996,7 @@ void Kangaroo::Run(int nbThread,std::vector<int> gpuId,std::vector<int> gridSize
// Fetch kangaroos (if any)
FectchKangaroos(params);

//#define STATS
#define STATS
#ifdef STATS

CPU_GRP_SIZE = 1024;
Expand Down Expand Up @@ -1057,7 +1052,7 @@ void Kangaroo::Run(int nbThread,std::vector<int> gpuId,std::vector<int> gridSize
double SN = pow(2.0,rangePower / 2.0);
double avg = (double)totalCount / (double)(keyIdx + 1);
::printf("\n[%3d] 2^%.3f Dead:%d Avg:2^%.3f DeadAvg:%.1f (%.3f %.3f sqrt(N))\n",
keyIdx, log2((double)count), collisionInSameHerd,
keyIdx, log2((double)count), (int)collisionInSameHerd,
log2(avg), (double)totalDead / (double)(keyIdx + 1),
avg/SN,expectedNbOp/SN);
}
Expand Down
13 changes: 12 additions & 1 deletion Kangaroo.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ typedef struct {

} DP;

typedef struct {

uint32_t header;
uint32_t nbDP;
uint32_t threadId;
uint32_t processId;
uint32_t gpuId;

} DPHEADER;

// DP cache
typedef struct {
uint32_t nbDP;
Expand Down Expand Up @@ -156,7 +166,7 @@ class Kangaroo {
void CreateJumpTable();
bool AddToTable(uint64_t h,int128_t *x,int128_t *d);
bool AddToTable(Int *pos,Int *dist,uint32_t kType);
bool SendToServer(std::vector<ITEM> &dp);
bool SendToServer(std::vector<ITEM> &dp,uint32_t threadId,uint32_t gpuId);
bool CheckKey(Int d1,Int d2,uint8_t type);
bool CollisionCheck(Int* d1,uint32_t type1,Int* d2,uint32_t type2);
void ComputeExpected(double dp,double *op,double *ram,double* overHead = NULL);
Expand Down Expand Up @@ -279,6 +289,7 @@ class Kangaroo {
std::vector<DP_CACHE> localCache;
std::string serverStatus;
int connectedClient;
uint32_t pid;

};

Expand Down
102 changes: 82 additions & 20 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ static SOCKET serverSock = 0;
#define WAIT_FOR_READ 1
#define WAIT_FOR_WRITE 2

#define SERVER_VERSION 2
#define SERVER_VERSION 3

#define SERVER_HEADER 0x67DEDDC1

// Commands
#define SERVER_GETCONFIG 0
Expand Down Expand Up @@ -307,13 +309,22 @@ bool Kangaroo::HandleRequest(TH_PARAM *p) {

case SERVER_SENDDP: {

uint32_t nbDP=0;
DPHEADER head;

GET("DPHeader",p->clientSock,&head,sizeof(DPHEADER),ntimeout);

if(head.header != SERVER_HEADER) {

::printf("\nUnexpected DP header from %s\n",p->clientInfo);
::printf("\nClosing connection with %s\n",p->clientInfo);
close_socket(p->clientSock);
return false;

GET("nbDP",p->clientSock,&nbDP,sizeof(uint32_t),ntimeout);
}

if(nbDP == 0) {
if(head.nbDP == 0) {

::printf("\nUnexpected number of DP [%d] from %s\n",nbDP,p->clientInfo);
::printf("\nUnexpected number of DP [%d] from %s\n",head.nbDP,p->clientInfo);
::printf("\nClosing connection with %s\n",p->clientInfo);
close_socket(p->clientSock);
return false;
Expand All @@ -322,38 +333,70 @@ bool Kangaroo::HandleRequest(TH_PARAM *p) {

//::printf("%d DP from %s\n",nbDP,p->clientInfo.c_str());

DP *dp = (DP *)malloc(sizeof(DP)*nbDP);
GETFREE("DP",p->clientSock,dp,sizeof(DP)*nbDP,ntimeout,dp);
DP *dp = (DP *)malloc(sizeof(DP)* head.nbDP);
GETFREE("DP",p->clientSock,dp,sizeof(DP)* head.nbDP,ntimeout,dp);
state = GetServerStatus();
PUTFREE("Status",p->clientSock,&state,sizeof(int32_t),ntimeout,dp);

if(nbRead != sizeof(DP)*nbDP) {
if(nbRead != sizeof(DP)* head.nbDP) {

::printf("\nUnexpected DP size from %s [nbDP=%d,Got %d,Expected %d]\n",p->clientInfo,nbDP,nbRead,(int)(sizeof(DP)*nbDP));
::printf("\nUnexpected DP size from %s [nbDP=%d,Got %d,Expected %d]\n",
p->clientInfo,head.nbDP,nbRead,(int)(sizeof(DP)* head.nbDP));
::printf("\nClosing connection with %s\n",p->clientInfo);
free(dp);
close_socket(p->clientSock);
return false;

} else {

#define VALIDITY_POINT_CHECK
#ifdef VALIDITY_POINT_CHECK
// Check validity
for(uint32_t i=0;i<nbDP;i++) {
for(uint32_t i=0;i< head.nbDP;i++) {

uint64_t h = (uint64_t)dp[i].h;
if(h >= HASH_SIZE) {
::printf("\nInvalid data from: %s [at dp=%d]\n",p->clientInfo,i);
::printf("\nInvalid data from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i,
head.processId,head.threadId,head.gpuId);
::printf("\nClosing connection with %s\n",p->clientInfo);
free(dp);
close_socket(p->clientSock);
return false;
}

Int dist;
uint32_t kType;
HashTable::CalcDistAndType(dp[i].d,&dist,&kType);
Point P = secp->ComputePublicKey(&dist);

if(kType == WILD)
P = secp->AddDirect(keyToSearch,P);

uint32_t hC = P.x.bits64[2] & HASH_MASK;
bool ok = (hC == h) && (P.x.bits64[0] == dp[i].x.i64[0]) && (P.x.bits64[1] == dp[i].x.i64[1]);
if(!ok) {
if(kType==TAME) {
::printf("\nWrong TAME point from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i,
head.processId,head.threadId,head.gpuId);
} else {
::printf("\nWrong WILD point from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i,
head.processId,head.threadId,head.gpuId);
}
//::printf("X=%s\n",P.x.GetBase16().c_str());
//::printf("X=%08X%08X%08X%08X\n",dp[i].x.i32[3],dp[i].x.i32[2],dp[i].x.i32[1],dp[i].x.i32[0]);
//::printf("D=%08X%08X%08X%08X\n",dp[i].d.i32[3],dp[i].d.i32[2],dp[i].d.i32[1],dp[i].d.i32[0]);
::printf("\nClosing connection with %s\n",p->clientInfo);
free(dp);
close_socket(p->clientSock);
return false;
}

}
#endif

LOCK(ghMutex);
DP_CACHE dc;
dc.nbDP = nbDP;
dc.nbDP = head.nbDP;
dc.dp = dp;
recvDP.push_back(dc);
UNLOCK(ghMutex);
Expand Down Expand Up @@ -463,11 +506,18 @@ void Kangaroo::RunServer() {
}
SetDP(initDPSize);

if(sizeof(DP)!=40) {
if(sizeof(DP) != 40) {
::printf("Error: Invalid DP size struct\n");
exit(-1);
}

if(sizeof(DPHEADER) != 20) {
::printf("Error: Invalid DPHEADER size struct\n");
exit(-1);
}



if(saveKangaroo) {
::printf("Waring: Server does not support -ws, ignoring\n");
saveKangaroo = false;
Expand Down Expand Up @@ -718,7 +768,7 @@ void Kangaroo::WaitForServer() {
}

// Send DP to Server
bool Kangaroo::SendToServer(std::vector<ITEM> &dps) {
bool Kangaroo::SendToServer(std::vector<ITEM> &dps,uint32_t threadId,uint32_t gpuId) {

int nbRead;
int nbWrite;
Expand Down Expand Up @@ -752,8 +802,15 @@ bool Kangaroo::SendToServer(std::vector<ITEM> &dps) {

char cmd = SERVER_SENDDP;

DPHEADER head;
head.header = SERVER_HEADER;
head.nbDP = nbDP;
head.processId = pid;
head.threadId = threadId;
head.gpuId = gpuId;

PUTFREE("CMD",serverConn,&cmd,1,ntimeout,dp);
PUTFREE("nbDP",serverConn,&nbDP,sizeof(uint32_t),ntimeout,dp);
PUTFREE("DPHeader",serverConn,&head,sizeof(DPHEADER),ntimeout,dp);
PUTFREE("DP",serverConn,dp,sizeof(DP)*nbDP,ntimeout,dp);
GETFREE("Status",serverConn,&status,sizeof(uint32_t),ntimeout,dp)

Expand Down Expand Up @@ -810,13 +867,18 @@ bool Kangaroo::GetConfigFromServer() {
GET("KeyY",serverConn,key.y.bits64,32,ntimeout);
GET("DP",serverConn,&initDPSize,sizeof(int32_t),ntimeout);

if(version>=2) {
// Set kangaroo number
char cmd = SERVER_SETKNB;
PUT("CMD",serverConn,&cmd,1,ntimeout);
PUT("nbKangaroo",serverConn,&totalRW,sizeof(uint64_t),ntimeout);
if(version<3) {
isConnected = false;
closesocket(serverConn);
::printf("Cannot connect to server: %s\nServer version must be >= 3\n",serverIp.c_str());
return false;
}

// Set kangaroo number
cmd = SERVER_SETKNB;
PUT("CMD",serverConn,&cmd,1,ntimeout);
PUT("nbKangaroo",serverConn,&totalRW,sizeof(uint64_t),ntimeout);

::printf("Succesfully connected to server: %s (Version %d)\n",serverIp.c_str(),version);

keysToSearch.clear();
Expand Down
16 changes: 12 additions & 4 deletions PartMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ bool Kangaroo::MergePartition(TH_PARAM* p) {
// Rename
string oldName = GetPartName(p1Name,part,true);
string newName = GetPartName(p1Name,part,false);
remove(newName.c_str());
rename(oldName.c_str(),newName.c_str());
if(!endOfSearch) {
remove(newName.c_str());
rename(oldName.c_str(),newName.c_str());
} else {
remove(oldName.c_str());
}

return true;

Expand Down Expand Up @@ -700,8 +704,12 @@ bool Kangaroo::MergeWorkPart(std::string& partName,std::string& file2,bool print
// Rename
string oldName = GetPartName(partName,part,true);
string newName = GetPartName(partName,part,false);
remove(newName.c_str());
rename(oldName.c_str(),newName.c_str());
if(!endOfSearch) {
remove(newName.c_str());
rename(oldName.c_str(),newName.c_str());
} else {
remove(oldName.c_str());
}

}

Expand Down
Loading

0 comments on commit 3727236

Please sign in to comment.