Skip to content

Commit

Permalink
support por Compiler microarchitecture optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Apr 11, 2018
1 parent 466e9c2 commit ec4e101
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 78 deletions.
6 changes: 0 additions & 6 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
37 changes: 15 additions & 22 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import copy
import re
import platform

import cpuid

def get_content(path):
with open(path, 'r') as f:
Expand All @@ -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())
Expand Down Expand Up @@ -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]):

Expand All @@ -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()
2 changes: 1 addition & 1 deletion conan_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3
0.4
85 changes: 50 additions & 35 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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"]
Expand All @@ -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'", \
Expand Down Expand Up @@ -188,18 +183,32 @@ 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)

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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit ec4e101

Please sign in to comment.