Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ txt/
bug/
prpll-debug
prpll-release
build/
.vscode/
.vs/
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.16)

project(gpuowl LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(src)
add_subdirectory(src)
60 changes: 60 additions & 0 deletions genbundle.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# genbundle.sh for crossplatform cmake
separate_arguments(CL_SOURCES)
message("CL_SOURCES = ${CL_SOURCES}")

file(WRITE "${OUTPUT}" "// bundle.cpp generated with genbundle.cmake\n")
file(APPEND "${OUTPUT}" "// Required CMAKE 3.16+ (for windows MSVC 17.0+)\n")
file(APPEND "${OUTPUT}" "// Works with Windows 10/11 | Linux/UNIX\n")
file(APPEND "${OUTPUT}" "// by CrazyDragon(GoodDrakon4ik)\n")
file(APPEND "${OUTPUT}" "// Copyright (C) Mihai Preda\n")
file(APPEND "${OUTPUT}" "// Generated file, do not edit. See genbundle.cmake and src/cl/*.cl\n\n")
file(APPEND "${OUTPUT}" "#include <vector>\n")
file(APPEND "${OUTPUT}" "static const std::vector<const char*> CL_FILES{\n")

set(names "")
set(file_count 0)
foreach(cl_file IN LISTS CL_SOURCES)
get_filename_component(fname "${cl_file}" NAME)
file(READ "${cl_file}" cl_content)

set(chunk_size 15000)
string(LENGTH "${cl_content}" len)
math(EXPR num_chunks "${len} / ${chunk_size} + 1")
message(STATUS "File: ${cl_file} | Lenght: ${len} | Needed chunks = ${num_chunks}")

file(APPEND "${OUTPUT}" "// ${cl_file}\n")

set(pos 0)
foreach(i RANGE 0 ${num_chunks})
math(EXPR start_pos "${i} * ${chunk_size}")
math(EXPR remaning "${len} - ${start_pos}")
if (remaning LESS 1)
break()
endif()
if (remaning GREATER ${chunk_size})
math(EXPR this_size "${chunk_size}")
else()
set(this_size "${remaning}")
endif()
string(SUBSTRING "${cl_content}" ${start_pos} ${this_size} chunk)
if (i EQUAL 0)
file(APPEND "${OUTPUT}" "R\"cltag(${chunk})cltag\"")
else()
file(APPEND "${OUTPUT}" "\n R\"cltag(${chunk})cltag\"")
endif()
message(STATUS "Chunk: ${i} - generated.")
endforeach()
math(EXPR file_count "${file_count} + 1")
list(LENGTH CL_SOURCES total_files)
if (NOT file_count EQUAL total_files)
file(APPEND "${OUTPUT}" ",\n")
else()
file(APPEND "${OUTPUT}" "\n")
endif()
set(names "${names}\"${fname}\",")
endforeach()

file(APPEND "${OUTPUT}" "};\n\n")
file(APPEND "${OUTPUT}" "static const std::vector<const char*> CL_FILE_NAMES{${names}};\n\n")
file(APPEND "${OUTPUT}" "const std::vector<const char*>& getClFileNames() { return CL_FILE_NAMES; }\n")
file(APPEND "${OUTPUT}" "const std::vector<const char*>& getClFiles() { return CL_FILES; }\n")
68 changes: 40 additions & 28 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(CMAKE_CXX_COMPILE g++-14)
endif()

#Replace add_custom_command version.inc
execute_process(
COMMAND git describe --tags --long --dirty --always --match v/prpll/*
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

if (NOT GIT_VERSION)
set(GIT_VERSION "unknown")
message(WARNING "Cant take version, using default value")
endif()

set(GIT_VERSION_QUOTED "\"${GIT_VERSION}\"")

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/version.inc" "${GIT_VERSION_QUOTED}\n")

file(GLOB CL_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/cl/*.cl")

set(bundle_cpp "${CMAKE_CURRENT_BINARY_DIR}/bundle.cpp")

add_custom_command(
OUTPUT ${bundle_cpp}
COMMAND ${CMAKE_COMMAND} -DCL_SOURCES="${CL_SOURCES}" -DOUTPUT=${bundle_cpp} -P ${CMAKE_SOURCE_DIR}/genbundle.cmake
DEPENDS ${CL_SOURCES}
COMMENT "Generation bundle.cpp"
)

add_library(bundle STATIC ${bundle_cpp})

add_executable(prpll
Trig.cpp
Primes.cpp
bundle.cpp
Proof.cpp
log.cpp md5.cpp sha3.cpp AllocTrac.cpp FFTConfig.cpp Worktodo.cpp common.cpp main.cpp Gpu.cpp clwrap.cpp Task.cpp timeutil.cpp Args.cpp state.cpp Signal.cpp
File.cpp
Expand All @@ -21,39 +54,18 @@ add_executable(prpll
TuneEntry.cpp
fs.cpp
version.inc
)
)

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Real GCC (not clang), needed for 128-bit floats and std::filesystem::path
set(CMAKE_CXX_COMPILER g++-14)
endif()
target_link_libraries(prpll PRIVATE bundle)

find_package(OpenCL)

if (${OpenCL_FOUND})
if (OpenCL_FOUND)
# Use OpenCL library found by cmake
target_link_libraries(prpll OpenCL::OpenCL)
target_link_libraries(prpll PRIVATE OpenCL::OpenCL)
else()
# Pass -lOpenCL to the linker and hope for the best
target_link_libraries(prpll OpenCL)
target_link_libraries(prpll PRIVATE OpenCL)
endif()

target_include_directories(prpll PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

add_custom_command(
OUTPUT version.inc
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND git describe --tags --long --dirty --always --match ${CMAKE_SOURCE_DIR} | sed 's/.*/"&"/' > ${CMAKE_CURRENT_BINARY_DIR}/version.inc
DEPENDS ${CMAKE_SOURCE_DIR}
)

file(
GLOB CL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cl/*.cl
)

add_custom_command(
OUTPUT bundle.cpp
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_SOURCE_DIR}/genbundle.sh ${CL_SOURCES} > ${CMAKE_CURRENT_BINARY_DIR}/bundle.cpp
DEPENDS ${CL_SOURCES}
)
target_include_directories(prpll PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
15 changes: 9 additions & 6 deletions src/FFTConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ FFTConfig::FFTConfig(const string& spec) {
// assert(v.size() == 1 || v.size() == 3 || v.size() == 4 || v.size() == 5);

if (v.size() == 1) {
*this = {FFTShape::multiSpec(spec).front(), 3, CARRY_AUTO};
*this = {FFTShape::multiSpec(spec).front(), 3, static_cast<u32>(CARRY_AUTO)};
} if (v.size() == 3) {
*this = {FFTShape{v[0], v[1], v[2]}, 3, CARRY_AUTO};
*this = {FFTShape{v[0], v[1], v[2]}, 3, static_cast<u32>(CARRY_AUTO)};
} else if (v.size() == 4) {
*this = {FFTShape{v[0], v[1], v[2]}, parseInt(v[3]), CARRY_AUTO};
*this = {FFTShape{v[0], v[1], v[2]}, parseInt(v[3]), static_cast<u32>(CARRY_AUTO)};
} else if (v.size() == 5) {
int c = parseInt(v[4]);
assert(c == 0 || c == 1);
*this = {FFTShape{v[0], v[1], v[2]}, parseInt(v[3]), c == 0 ? CARRY_32 : CARRY_64};
*this = {FFTShape{v[0], v[1], v[2]}, parseInt(v[3]), static_cast<u32>(c == 0 ? CARRY_32 : CARRY_64)};
} else {
throw "FFT spec";
}
Expand All @@ -175,7 +175,7 @@ FFTConfig::FFTConfig(FFTShape shape, u32 variant, u32 carry) :

string FFTConfig::spec() const {
string s = shape.spec() + ":" + to_string(variant);
return carry == CARRY_AUTO ? s : (s + (carry == CARRY_32 ? ":0" : ":1"));
return carry == static_cast<u32>(CARRY_AUTO) ? s : (s + (carry == CARRY_32 ? ":0" : ":1"));
}

double FFTConfig::maxBpw() const {
Expand Down Expand Up @@ -205,7 +205,10 @@ FFTConfig FFTConfig::bestFit(const Args& args, u32 E, const string& spec) {
// Take the first FFT that can handle E
for (const FFTShape& shape : FFTShape::allShapes()) {
for (u32 v = 0; v < 4; ++v) {
if (FFTConfig fft{shape, v, CARRY_AUTO}; fft.maxExp() * args.fftOverdrive >= E) { return fft; }
if (FFTConfig fft{shape, v, static_cast<u32>(CARRY_AUTO)}; fft.maxExp() * args.fftOverdrive >= E)
{
return fft;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using namespace std;

File::File(const std::filesystem::__cxx11::path& path, const string& mode, bool throwOnError)
File::File(const std::filesystem::path& path, const string& mode, bool throwOnError)
: readOnly{mode == "rb"}, name{path.string()} {
assert(readOnly || throwOnError);

Expand Down
48 changes: 29 additions & 19 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@
#include <cstdio>
#include <cstdarg>
#include <cassert>
#include <unistd.h>
//! Support for CrossPlatform
#if defined(_WIN32) || defined(__WIN32__)
#include <io.h> //? windows
#elif defined(__APPLE__)
#include <fcntl.h> //? apple
#else
#include <unistd.h> //? linux/unix
#endif
//! End
#include <filesystem>
#include <vector>
#include <string>
#include <optional>

#if defined(_WIN32) || defined(__WIN32__)
#include <io.h>
#endif

#if defined(__APPLE__)
#include <fcntl.h>
#endif

#if defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE)
#define HAS_SETLINEBUF 1
#else
#define HAS_SETLINEBUF 0
#endif

//! Macros for __attribute__ compiller/scrossplatform support
#if defined(__GNUC__) || defined(__clang__)
#define FORMAT_PRINTF(fmt_idx, arg_idx) __attribute__((format(printf, fmt_idx, arg_idx)))
#define FORMAT_SCANF(fmt_idx, arg_idx) __attribute__((format(scanf, fmt_idx, arg_idx)))
#else
#define FORMAT_PRINTF(fmt_idx, arg_idx)
#define FORMAT_SCANF(fmt_idx, arg_idx)
#endif
//! End

namespace fs = std::filesystem;

struct CRCError {
Expand All @@ -48,7 +58,7 @@ class File {

File(const fs::path &path, const string& mode, bool throwOnError);

bool readNoThrow(void* data, u32 nBytes) const { return fread(data, nBytes, 1, get()); }
bool readNoThrow(void *data, u32 nBytes) const { return fread(data, nBytes, 1, this->get()); }

void read(void* data, u32 nBytes) const {
if (!readNoThrow(data, nBytes)) { throw ReadError{name}; }
Expand Down Expand Up @@ -121,19 +131,19 @@ class File {
template<typename T>
void write(const T& x) const { write(&x, sizeof(T)); }

void write(const void* data, u32 nBytes) const {
if (!fwrite(data, nBytes, 1, get())) { throw WriteError{name}; }
void write(const void *data, u32 nBytes) const {
if (!fwrite(data, nBytes, 1, this->get())) { throw WriteError{name}; }
}

void seek(long offset, int whence = SEEK_SET) {
int ret = fseek(get(), offset, whence);
int ret = fseek(this->get(), offset, whence);
if (ret) { throw ReadError{name}; }
// throw(std::ios_base::failure(("fseek: "s + to_string(ret)).c_str()));
}

void flush() { fflush(get()); }
void flush() { fflush(this->get()); }

int printf(const char *fmt, ...) const __attribute__((format(printf, 2, 3))) {
int printf(const char *fmt, ...) const FORMAT_PRINTF(2, 3) {
va_list va;
va_start(va, fmt);
int ret = vfprintf(f, fmt, va);
Expand All @@ -146,7 +156,7 @@ class File {
return ret;
}

int scanf(const char *fmt, ...) __attribute__((format(scanf, 2, 3))) {
int scanf(const char *fmt, ...) FORMAT_SCANF(2, 3) {
va_list va;
va_start(va, fmt);
int ret = vfscanf(f, fmt, va);
Expand All @@ -162,7 +172,7 @@ class File {
FILE* get() const { return f; }

long ftell() const {
long pos = ::ftell(get());
long pos = ::ftell(this->get());
assert(pos >= 0);
return pos;
}
Expand All @@ -185,7 +195,7 @@ class File {
std::string readLine() {
char buf[1024];
buf[0] = 0;
bool ok = fgets(buf, sizeof(buf), get());
bool ok = fgets(buf, sizeof(buf), this->get());
if (!ok) { return ""; } // EOF or error
string line = buf;
if (line.empty() || line.back() != '\n') {
Expand Down Expand Up @@ -239,7 +249,7 @@ class File {
return data;
}

u32 readUpTo(void* data, u32 nUpToBytes) { return fread(data, 1, nUpToBytes, get()); }
u32 readUpTo(void *data, u32 nUpToBytes) { return fread(data, 1, nUpToBytes, this->get()); }

string readAll() {
size_t sz = size();
Expand Down
3 changes: 1 addition & 2 deletions src/Gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <cmath>

struct PRPResult;
struct Task;
class Task;

class Signal;
class ProofSet;
Expand Down Expand Up @@ -299,7 +299,6 @@ class Gpu {
vector<u32> readCheck();
vector<u32> readData();


u32 getFFTSize() { return N; }

// return A^h * B
Expand Down
6 changes: 6 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
//! Support for CrossPlatform
#if defined(_WIN32) || defined(__WIN32__)
// using f128 = long double; #add only if need to be used for compilation. __float128 didnt support by MSVC
#else
using f128 = __float128;
#endif
//! End

static_assert(sizeof(u8) == 1, "size u8");
static_assert(sizeof(u32) == 4, "size u32");
Expand Down
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <filesystem>
#include <thread>
#include <cstdlib>
// #include <format> from GCC-13 onwards

namespace fs = std::filesystem;
Expand All @@ -41,15 +42,20 @@ void gpuWorker(GpuCommon shared, Queue *q, i32 instance) {
}
}


#ifdef __MINGW32__ // for Windows
extern int putenv(const char *);
#endif

int main(int argc, char **argv) {
//!MSVC version support
#ifdef _MSC_VER
_set_printf_count_output(1);
#endif

#ifdef __MINGW32__
putenv("ROC_SIGNAL_POOL_SIZE=32");
#elif defined(_WIN32)
_putenv_s("ROC_SIGNAL_POOL_SIZE", "32");
#else
// Required to work around a ROCm bug when using multiple queues
setenv("ROC_SIGNAL_POOL_SIZE", "32", 0);
Expand Down
Loading