Skip to content

Commit

Permalink
Merge pull request #29 from dave-andersen/master
Browse files Browse the repository at this point in the history
Hugepages for Linux speed optimization + fixing MacOS build
  • Loading branch information
cryptozoidberg committed Jun 23, 2014
2 parents 1ada156 + ea2db5c commit 9e31e74
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 31 deletions.
32 changes: 16 additions & 16 deletions src/crypto/alt/KeccakF-1600-opt64.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ typedef unsigned char UINT8;


#if defined(__GNUC__)
#define ALIGN __attribute__ ((aligned(32)))
#define KALIGN __attribute__ ((aligned(32)))
#elif defined(_MSC_VER)
#define ALIGN __declspec(align(32))
#define KALIGN __declspec(align(32))
#else
#define ALIGN
#define KALIGN
#endif

#if defined(UseSSE)
Expand Down Expand Up @@ -67,7 +67,7 @@ typedef unsigned char UINT8;
#ifdef UseOnlySIMD64
#include "KeccakF-1600-simd64.macros"
#else
ALIGN const UINT64 rho8_56[2] = {0x0605040302010007, 0x080F0E0D0C0B0A09};
KALIGN const UINT64 rho8_56[2] = {0x0605040302010007, 0x080F0E0D0C0B0A09};
#include "KeccakF-1600-simd128.macros"
#endif

Expand Down Expand Up @@ -105,18 +105,18 @@ ALIGN const UINT64 rho8_56[2] = {0x0605040302010007, 0x080F0E0D0C0B0A09};

#define ROL6464same(a, o) _mm_roti_epi64(a, o)
#define ROL6464(a, r1, r2) _mm_rot_epi64(a, CONST128( rot_##r1##_##r2 ))
ALIGN const UINT64 rot_0_20[2] = { 0, 20};
ALIGN const UINT64 rot_44_3[2] = {44, 3};
ALIGN const UINT64 rot_43_45[2] = {43, 45};
ALIGN const UINT64 rot_21_61[2] = {21, 61};
ALIGN const UINT64 rot_14_28[2] = {14, 28};
ALIGN const UINT64 rot_1_36[2] = { 1, 36};
ALIGN const UINT64 rot_6_10[2] = { 6, 10};
ALIGN const UINT64 rot_25_15[2] = {25, 15};
ALIGN const UINT64 rot_8_56[2] = { 8, 56};
ALIGN const UINT64 rot_18_27[2] = {18, 27};
ALIGN const UINT64 rot_62_55[2] = {62, 55};
ALIGN const UINT64 rot_39_41[2] = {39, 41};
KALIGN const UINT64 rot_0_20[2] = { 0, 20};
KALIGN const UINT64 rot_44_3[2] = {44, 3};
KALIGN const UINT64 rot_43_45[2] = {43, 45};
KALIGN const UINT64 rot_21_61[2] = {21, 61};
KALIGN const UINT64 rot_14_28[2] = {14, 28};
KALIGN const UINT64 rot_1_36[2] = { 1, 36};
KALIGN const UINT64 rot_6_10[2] = { 6, 10};
KALIGN const UINT64 rot_25_15[2] = {25, 15};
KALIGN const UINT64 rot_8_56[2] = { 8, 56};
KALIGN const UINT64 rot_18_27[2] = {18, 27};
KALIGN const UINT64 rot_62_55[2] = {62, 55};
KALIGN const UINT64 rot_39_41[2] = {39, 41};

#if defined(UseSimulatedXOP)
// For debugging purposes, when XOP is not available
Expand Down
12 changes: 6 additions & 6 deletions src/crypto/alt/KeccakSponge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ and related or neighboring rights to the source code in this file.
#define KeccakMaximumRateInBytes (KeccakMaximumRate/8)

#if defined(__GNUC__)
#define ALIGN __attribute__ ((aligned(32)))
#define KALIGN __attribute__ ((aligned(32)))
#elif defined(_MSC_VER)
#define ALIGN __declspec(align(32))
#define KALIGN __declspec(align(32))
#else
#define ALIGN
#define KALIGN
#endif

ALIGN typedef struct spongeStateStruct {
ALIGN unsigned char state[KeccakPermutationSizeInBytes];
ALIGN unsigned char dataQueue[KeccakMaximumRateInBytes];
KALIGN typedef struct spongeStateStruct {
KALIGN unsigned char state[KeccakPermutationSizeInBytes];
KALIGN unsigned char dataQueue[KeccakMaximumRateInBytes];
unsigned int rate;
unsigned int capacity;
unsigned int bitsInQueue;
Expand Down
16 changes: 8 additions & 8 deletions src/currency_core/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace currency
CRITICAL_REGION_LOCAL(m_aliace_to_apply_in_block_lock);
if(m_bc.get_alias_info(m_alias_to_apply_in_block.m_alias, dummy))
{
LOG_PRINT_L0("Alias \"" << m_alias_to_apply_in_block.m_alias << "\" got used bye someone else, canceled." );
LOG_PRINT_L0("Alias \"" << m_alias_to_apply_in_block.m_alias << "\" got used by someone else, canceled." );
m_alias_to_apply_in_block = AUTO_VAL_INIT(m_alias_to_apply_in_block);
return false;
}
Expand Down Expand Up @@ -256,14 +256,14 @@ namespace currency
{
LOG_PRINT_CYAN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", LOG_LEVEL_0);
LOG_PRINT_L0(ENDL << "**********************************************************************" << ENDL
<< "NOTICE: Each block in blockchain have a vote, that takes into account "<< ENDL
<< "when calculating the reward amount for project team(once a day)." << ENDL
<< "NOTICE: Each block in the blockchain has a vote that is taken into account "<< ENDL
<< "when calculating the reward amount for project team (once per day)." << ENDL
<< ENDL
<< "Be sure to specify an option that expresses your attitude to work that make the project developers." << ENDL
<< "Be sure to specify an option that expresses your attitude to work by the project developers." << ENDL
<< "If you support the project, leave donations enabled. If you disagree with the actions of the team,"
<< "vote against donations.(by entering command \"set_donations false\")" << ENDL
<< "vote against donations (by entering command \"set_donations false\")." << ENDL
<< ENDL
<< "By default, if you didn't refuse it explicitely, donations set to enabled." << ENDL
<< "By default, if you don't disable them explicitly, donations will be enabled." << ENDL
<< ENDL
<< "**********************************************************************");
LOG_PRINT_CYAN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", LOG_LEVEL_0);
Expand All @@ -276,13 +276,13 @@ namespace currency
CRITICAL_REGION_LOCAL(m_threads_lock);
if(is_mining())
{
LOG_ERROR("Starting miner but it's already started");
LOG_ERROR("Starting miner canceled: miner already running");
return false;
}

if(!m_threads.empty())
{
LOG_ERROR("Unable to start miner because there are active mining threads");
LOG_ERROR("Starting miner canceled: there are still active mining threads");
return false;
}

Expand Down
53 changes: 52 additions & 1 deletion src/miner/simpleminer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "currency_core/account.h"
#include "currency_core/currency_format_utils.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include <sys/mman.h>

using namespace epee;
namespace po = boost::program_options;
Expand Down Expand Up @@ -101,6 +102,8 @@ namespace mining
m_pass = command_line::get_arg(vm, arg_pass);
m_hi = AUTO_VAL_INIT(m_hi);
m_last_job_ticks = 0;
m_fast_scratchpad_pages = 0;
m_fast_scratchpad = NULL;

return true;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ namespace mining
crypto::hash h = currency::null_hash;
currency::get_blob_longhash(blob, h, m_job.prev_hi.height+1, [&](uint64_t index) -> crypto::hash&
{
return m_scratchpad[index%m_scratchpad.size()];
return m_fast_scratchpad[index%m_scratchpad.size()];
});

if( currency::check_hash(h, m_job.difficulty))
Expand Down Expand Up @@ -237,6 +240,7 @@ namespace mining

uint64_t get_job_start_time = epee::misc_utils::get_tick_count();
get_job(); /* Next version: Handle this asynchronously */
update_fast_scratchpad();
uint64_t get_job_end_time = epee::misc_utils::get_tick_count();
if ((get_job_end_time - get_job_start_time) > 1000)
{
Expand Down Expand Up @@ -336,6 +340,7 @@ namespace mining
if (job_submit_failures == 5)
{
LOG_PRINT_L0("Too many submission failures. Something is very wrong.");
free_fast_scratchpad();
return false;
}
if (job_submit_failures > 3)
Expand All @@ -353,6 +358,7 @@ namespace mining
LOG_PRINT_L0("avg hr: " << hash_rate);
}
}
free_fast_scratchpad();
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -388,6 +394,51 @@ namespace mining
LOG_PRINT_L0("Scratchpad received ok, size: " << (m_scratchpad.size()*32)/1024 << "Kb, heigh=" << m_hi.height);
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
void simpleminer::free_fast_scratchpad()
{
if (m_fast_scratchpad)
{
if (m_fast_mmapped)
{
munmap(m_fast_scratchpad, m_fast_scratchpad_pages*4096);
} else {
free(m_fast_scratchpad);
}
m_fast_scratchpad = NULL;
}
}

void simpleminer::update_fast_scratchpad()
{

/* Check size - reallocate fast scratch if necessary */
size_t cur_scratchpad_size = m_scratchpad.size() * sizeof(crypto::hash);
size_t cur_scratchpad_pages = (cur_scratchpad_size / 4096) + 1;
if (cur_scratchpad_pages > m_fast_scratchpad_pages)
{
free_fast_scratchpad();
void *addr = MAP_FAILED;
size_t mapsize = cur_scratchpad_pages * 4096;
#ifdef MAP_HUGETLB
addr = mmap(0, mapsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|MAP_POPULATE, 0, 0);
if (addr == MAP_FAILED)
{
LOG_PRINT_L0("Unable to mmap huge pages. Enable for faster operation.");
}
#endif
if (addr == MAP_FAILED) {
addr = malloc(mapsize);
} else {
m_fast_mmapped = true;
}
m_fast_scratchpad_pages = cur_scratchpad_pages;
m_fast_scratchpad = (crypto::hash *)addr;
}

memcpy(m_fast_scratchpad, &m_scratchpad[0], m_scratchpad.size() * sizeof(crypto::hash));
}

//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::pop_addendum(const addendum& add)
{
Expand Down
5 changes: 5 additions & 0 deletions src/miner/simpleminer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ namespace mining
bool pop_addendum(const addendum& add);
bool push_addendum(const addendum& add);
void worker_thread(uint64_t start_nonce, uint32_t nonce_offset, std::atomic<uint32_t> *result, std::atomic<bool> *do_reset, std::atomic<bool> *done);
void update_fast_scratchpad();
void free_fast_scratchpad();

std::vector<mining::addendum> m_blocks_addendums; //need to handle splits without re-downloading whole scratchpad
height_info_native m_hi;
std::vector<crypto::hash> m_scratchpad;
crypto::hash *m_fast_scratchpad;
uint32_t m_fast_scratchpad_pages;
uint64_t m_last_job_ticks;
bool m_fast_mmapped;
uint32_t m_threads_total;
std::atomic<uint64_t> m_hashes_done;
std::string m_pool_session_id;
Expand Down

0 comments on commit 9e31e74

Please sign in to comment.