From ec4e101baa396b8d5b74d6201ecaf94ad1d8206b Mon Sep 17 00:00:00 2001 From: Fernando Pelliccioni Date: Wed, 11 Apr 2018 09:29:59 -0300 Subject: [PATCH] support por Compiler microarchitecture optimizations --- .appveyor.yml | 6 --- .travis.yml | 8 ---- CMakeLists.txt | 14 ++++--- _test_package/conanfile.py | 2 +- build.py | 37 +++++++---------- conan_version | 2 +- conanfile.py | 85 ++++++++++++++++++++++---------------- 7 files changed, 76 insertions(+), 78 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 881102b8f2..c5238336ac 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -31,12 +31,6 @@ environment: PYTHON_VERSION: "2.7.8" PYTHON_ARCH: "32" - # CONAN_REFERENCE: "secp256k1/0.3" - # CONAN_USERNAME: "bitprim" - # CONAN_LOGIN_USERNAME: "bitprim-bintray" - # CONAN_CHANNEL: "testing" - # CONAN_UPLOAD: "https://api.bintray.com/conan/bitprim/bitprim" - matrix: - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 CONAN_VISUAL_VERSIONS: 14 diff --git a/.travis.yml b/.travis.yml index 01359144d8..8db9da3cff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,14 +25,6 @@ branches: - feature_compile_time_currencies -# env: -# global: -# - CONAN_REFERENCE: "secp256k1/0.3" -# - CONAN_USERNAME: "bitprim" -# - CONAN_LOGIN_USERNAME: "bitprim-bintray" -# - CONAN_CHANNEL: "testing" -# - CONAN_UPLOAD: "https://api.bintray.com/conan/bitprim/bitprim" - linux: &linux os: linux sudo: required diff --git a/CMakeLists.txt b/CMakeLists.txt index 5433212b08..d1344e5809 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2017 Bitprim developers (see AUTHORS) +# Copyright (c) 2017-2018 Bitprim developers (see AUTHORS) # # This file is part of Bitprim. # @@ -22,7 +22,7 @@ cmake_minimum_required(VERSION 3.4) # secp256k1 #============================================================================== project(secp256k1 - VERSION 0.3 + VERSION 0.4 LANGUAGES C) set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -61,9 +61,6 @@ option(ENABLE_MODULE_SCHNORR "Enable Schnorr signature module (experimental)." O option(ENABLE_MODULE_RECOVERY "Enable ECDSA pubkey recovery module." ON) # message(${ENABLE_TESTS}) - - - # option(WITH_BIGNUM "") #TODO(fernando): Implement the following with_... as options @@ -77,6 +74,13 @@ set(BITPRIM_BUILD_NUMBER "-" CACHE STRING "Specify the Bitprim Build Number.") # message(${BITPRIM_BUILD_NUMBER}) +set(MICROARCHITECTURE "x86_64" CACHE STRING "Specify the Cryptocurrency (x86_64|...).") +message( STATUS "Bitprim: Compiling for ${MICROARCHITECTURE}") +# add_definitions(-DBITPRIM_MICROARCHITECTURE=${MICROARCHITECTURE}) +# add_definitions(-DBITPRIM_MICROARCHITECTURE_STR="${MICROARCHITECTURE}") + + + # if (NOT NO_CONAN_AT_ALL) # if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) # include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) diff --git a/_test_package/conanfile.py b/_test_package/conanfile.py index aac283ae4b..a44379d699 100644 --- a/_test_package/conanfile.py +++ b/_test_package/conanfile.py @@ -8,7 +8,7 @@ class Secp256k1TestConan(ConanFile): settings = "os", "compiler", "build_type", "arch" - requires = "secp256k1/0.3@%s/%s" % (username, channel) + requires = "secp256k1/0.4@%s/%s" % (username, channel) generators = "cmake" # build_policy = "missing" diff --git a/build.py b/build.py index 17608cf693..f9462d96af 100644 --- a/build.py +++ b/build.py @@ -3,7 +3,7 @@ import copy import re import platform - +import cpuid def get_content(path): with open(path, 'r') as f: @@ -16,16 +16,6 @@ def get_channel(): return get_content('conan_channel') def get_conan_vars(): - # CONAN_REFERENCE: "bitprim-core/0.7" - # CONAN_USERNAME: "bitprim" - # CONAN_LOGIN_USERNAME: "bitprim-bintray" - # CONAN_CHANNEL: "experimental" - # CONAN_UPLOAD: "https://api.bintray.com/conan/bitprim/bitprim" - - # username = os.getenv("CONAN_USERNAME", get_username_from_ci() or "bitprim") - # channel = os.getenv("CONAN_CHANNEL", get_channel_from_ci()) - # version = os.getenv("CONAN_VERSION", get_version()) - login_username = os.getenv("CONAN_LOGIN_USERNAME", "bitprim-bintray") username = os.getenv("CONAN_USERNAME", "bitprim") channel = os.getenv("CONAN_CHANNEL", get_channel()) @@ -97,22 +87,22 @@ def get_builder(args=None): return builder, name -if __name__ == "__main__": - # builder = ConanMultiPackager(username="bitprim", channel="testing", - # remotes="https://api.bintray.com/conan/bitprim/bitprim", - # archs=["x86_64"]) +def handle_microarchs(opt_name, microarchs, filtered_builds, settings, options, env_vars, build_requires): + microarchs = list(set(microarchs)) + + for ma in microarchs: + opts_copy = copy.deepcopy(options) + opts_copy[opt_name] = ma + filtered_builds.append([settings, opts_copy, env_vars, build_requires]) - builder, name = get_builder() +if __name__ == "__main__": + builder, name = get_builder() builder.add_common_builds(shared_option_name="%s:shared" % name, pure_c=True) filtered_builds = [] for settings, options, env_vars, build_requires in builder.builds: - # if settings["build_type"] == "Release" \ - # and not("%s:shared" % name in options and options["%s:shared" % name]) \ - # and (not "compiler.runtime" in settings or not settings["compiler.runtime"] == "MT"): - if settings["build_type"] == "Release" \ and not("%s:shared" % name in options and options["%s:shared" % name]): @@ -122,8 +112,11 @@ def get_builder(args=None): # options["%s:with_benchmark" % name] = "True" options["%s:with_tests" % name] = "True" # options["%s:with_openssl_tests" % name] = "True" - - filtered_builds.append([settings, options, env_vars, build_requires]) + + marchs = ["x86_64", ''.join(cpuid.cpu_microarchitecture()), "haswell", "skylake", "skylake-avx512"] + handle_microarchs("%s:microarchitecture" % name, marchs, filtered_builds, settings, options, env_vars, build_requires) + + # filtered_builds.append([settings, options, env_vars, build_requires]) builder.builds = filtered_builds builder.run() diff --git a/conan_version b/conan_version index 1d71ef9744..e6adf3fc7b 100644 --- a/conan_version +++ b/conan_version @@ -1 +1 @@ -0.3 \ No newline at end of file +0.4 \ No newline at end of file diff --git a/conanfile.py b/conanfile.py index 63fc241cb6..66c485c696 100644 --- a/conanfile.py +++ b/conanfile.py @@ -21,43 +21,13 @@ from conans import ConanFile, CMake from conans import __version__ as conan_version from conans.model.version import Version +import importlib def option_on_off(option): return "ON" if option else "OFF" - -# def make_default_options_method(): -# defs = "shared=False", \ -# "fPIC=True", \ -# "with_benchmark=False", \ -# "with_tests=True", \ -# "with_openssl_tests=False", \ -# "enable_experimental=False", \ -# "enable_endomorphism=False", \ -# "enable_ecmult_static_precomputation=True", \ -# "enable_module_ecdh=False", \ -# "enable_module_schnorr=False", \ -# "enable_module_recovery=True", \ -# "with_bignum=conan" - -# # "with_asm='auto'", \ -# # "with_field='auto'", \ -# # "with_scalar='auto'" -# # "with_bignum='auto'" - - -# if cpuid_installed: -# gmp_opt = "gmp:microarchitecture=%s" % (''.join(cpuid.cpu_microarchitecture())) -# new_defs = defs + (gmp_opt,) -# return new_defs - -# return defs def get_content(file_name): - # print(os.path.dirname(os.path.abspath(__file__))) - # print(os.getcwd()) - # with open(path, 'r') as f: - # return f.read() file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name) with open(file_path, 'r') as f: return f.read() @@ -71,6 +41,29 @@ def get_channel(): def get_conan_req_version(): return get_content('conan_req_version') +microarchitecture_default = 'x86_64' + +def get_cpuid(): + try: + # print("*** cpuid OK") + cpuid = importlib.import_module('cpuid') + return cpuid + except ImportError: + # print("*** cpuid could not be imported") + return None + +def get_cpu_microarchitecture_or_default(default): + cpuid = get_cpuid() + if cpuid != None: + # return '%s%s' % cpuid.cpu_microarchitecture() + return '%s' % (''.join(cpuid.cpu_microarchitecture())) + else: + return default + +def get_cpu_microarchitecture(): + return get_cpu_microarchitecture_or_default(microarchitecture_default) + + class Secp256k1Conan(ConanFile): name = "secp256k1" version = get_version() @@ -97,6 +90,8 @@ class Secp256k1Conan(ConanFile): "with_tests": [True, False], "with_openssl_tests": [True, False], "with_bignum_lib": [True, False], + "microarchitecture": "ANY", #["x86_64", "haswell", "ivybridge", "sandybridge", "bulldozer", ...] + "verbose": [True, False], # "with_bignum": ["conan", "auto", "system", "no"] @@ -122,9 +117,9 @@ class Secp256k1Conan(ConanFile): "with_benchmark=False", \ "with_tests=False", \ "with_openssl_tests=False", \ - "with_bignum_lib=True" - - + "with_bignum_lib=True", \ + "microarchitecture=_DUMMY_", \ + "verbose=True" # "with_bignum=conan" # "with_asm='auto'", \ @@ -188,10 +183,24 @@ def config_options(self): def configure(self): del self.settings.compiler.libcxx #Pure-C Library + if self.options.microarchitecture == "_DUMMY_": + self.options.microarchitecture = get_cpu_microarchitecture() + if get_cpuid() == None: + march_from = 'default' + else: + march_from = 'taken from cpuid' + else: + march_from = 'user defined' + + # self.options["*"].microarchitecture = self.options.microarchitecture + self.output.info("Compiling for microarchitecture (%s): %s" % (march_from, self.options.microarchitecture)) + + def package_id(self): self.info.options.with_benchmark = "ANY" self.info.options.with_tests = "ANY" self.info.options.with_openssl_tests = "ANY" + self.info.options.verbose = "ANY" def build(self): cmake = CMake(self) @@ -199,7 +208,7 @@ def build(self): cmake.definitions["USE_CONAN"] = option_on_off(True) cmake.definitions["NO_CONAN_AT_ALL"] = option_on_off(False) # cmake.definitions["CMAKE_VERBOSE_MAKEFILE"] = option_on_off(False) - cmake.verbose = False + cmake.verbose = self.options.verbose cmake.definitions["ENABLE_SHARED"] = option_on_off(self.is_shared) cmake.definitions["ENABLE_POSITION_INDEPENDENT_CODE"] = option_on_off(self.fPIC_enabled) @@ -225,6 +234,8 @@ def build(self): cmake.definitions["WITH_BIGNUM"] = self.bignum_lib_name + cmake.definitions["MICROARCHITECTURE"] = self.options.microarchitecture + if self.settings.os == "Windows": if self.settings.compiler == "Visual Studio" and (self.settings.compiler.version != 12): cmake.definitions["ENABLE_TESTS"] = option_on_off(False) #Workaround. test broke MSVC @@ -245,6 +256,10 @@ def build(self): # cmake.definitions["WITH_SCALAR"] = option_on_off(self.options.with_scalar) # cmake.definitions["WITH_BIGNUM"] = option_on_off(self.options.with_bignum) + if self.settings.compiler != "Visual Studio": + cmake.definitions["CONAN_CXX_FLAGS"] = cmake.definitions.get("CONAN_CXX_FLAGS", "") + " -march=" + self.options.microarchitecture + + # microarchitecture_default cmake.definitions["BITPRIM_BUILD_NUMBER"] = os.getenv('BITPRIM_BUILD_NUMBER', '-') cmake.configure(source_dir=self.source_folder)