gpt4 book ai didi

c++ - 编译可在Mac上运行,但不能在群集上运行(Linux)

转载 作者:行者123 更新时间:2023-12-02 10:13:32 25 4
gpt4 key购买 nike

我有一个使用c++ 17编写的库,该库可以在我的Mac上编译,但不能在我的大学(Linux)的群集上编译。
我的编译器是:
Apple clang版本11.0.0(clang-1100.0.33.17)
集群编译器为:
g++(Spack GCC)9.2.0
我的一位同事还安装了该库,并成功地在其计算机(始终为Mac)上对其进行了编译。
我试图将所有内容减少到主要(我想)错误,并将所有内容都放在下面的代码中。
这是错误:

error: invalid use of incomplete type 'class TensorBase<double, std::integer_sequence<long unsigned int, 0> >'
116 | class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_index_sequence<1>>
下面我放代码。我基本上有一个 BaseTensor类,该类是 Matrix继承的。在此类类(class)中,我也有专长。
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <array>


using Integer=long;
using Real=double;



template <typename T, std::size_t>
using getTypeSequence = T;



template <typename, typename>
class TensorBase;

template <typename T, Integer ... Is>
class TensorBase<T, std::index_sequence<Is...>>
{
protected:
std::array<T, sizeof...(Is)> values{};
static const std::size_t Size = sizeof...(Is);

public:
constexpr TensorBase (getTypeSequence<T, Is> ... vals)
: values{{vals...}}
{}

constexpr TensorBase (std::array<T, sizeof...(Is)> const & a)
: values{a}
{}

constexpr TensorBase (std::array<T, sizeof...(Is)> && a)
: values{std::move(a)}
{}

// TensorBase(std::initializer_list<T> a)
// {
// assert(a.size() == Size);
// std::copy(std::begin(a), std::end(a), std::begin(values));
// }

constexpr TensorBase () = default;

~TensorBase() = default;

constexpr TensorBase (TensorBase const &) = default;
constexpr TensorBase (TensorBase &&) = default;

constexpr TensorBase & operator= (TensorBase const &) = default;
constexpr TensorBase & operator= (TensorBase &&) = default;
};


template<typename T, Integer Rows_, Integer Cols_,Integer NonZeroRow_=-1>
class Matrix: public TensorBase<T, std::make_index_sequence<Rows_*Cols_>>
{
public:
static constexpr Integer Rows=Rows_;
static constexpr Integer Cols=Cols_;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }
inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}

inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}
// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}

inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
};





template<Integer NonZeroRow_>
class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_index_sequence<1>>
{
public:
static constexpr Integer Rows=1;
static constexpr Integer Cols=1;
using T= Real;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }

inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}

inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}

inline constexpr T &operator[](const Integer i)
{
assert(i < Rows);
return values[i];
}

inline constexpr T &operator[](const Integer i)const
{
assert(i < Rows);
return values[i];
}


// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}

inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}


};

int main(int argc, char *argv[])
{

constexpr Matrix<Real,1,1> mat{1.0};


std::cout<<"it works"<<std::endl;
static_assert(mat(0,0)==1.0);



return 0;
}

最佳答案

使用integer_sequence<Integer, Is...>而不是index_sequence<Is...>并将const添加到operator[]中的Matrix<Real,1,1,NonZeroRow_>返回中,修复了我在gcc 10.1中的构建错误。我真的不知道为什么使用整数序列来解决问题,但是除了解决构建错误外,这种情况下的行为也应相同。
这是我对https://godbolt.org/z/dKHLDB进行编辑后编译的版本

#include <cstdlib>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <array>


using Integer=long;
using Real=double;



template <typename T, std::size_t>
using getTypeSequence = T;



template <typename, typename>
class TensorBase;

template <typename T, Integer ... Is>
class TensorBase<T, std::integer_sequence<Integer, Is...>>
{
protected:
std::array<T, sizeof...(Is)> values{};
static const std::size_t Size = sizeof...(Is);

public:
constexpr TensorBase (getTypeSequence<T, Is> ... vals)
: values{{vals...}}
{}

constexpr TensorBase (std::array<T, sizeof...(Is)> const & a)
: values{a}
{}

constexpr TensorBase (std::array<T, sizeof...(Is)> && a)
: values{std::move(a)}
{}

// TensorBase(std::initializer_list<T> a)
// {
// assert(a.size() == Size);
// std::copy(std::begin(a), std::end(a), std::begin(values));
// }

constexpr TensorBase () = default;

~TensorBase() = default;

constexpr TensorBase (TensorBase const &) = default;
constexpr TensorBase (TensorBase &&) = default;

constexpr TensorBase & operator= (TensorBase const &) = default;
constexpr TensorBase & operator= (TensorBase &&) = default;
};


template<typename T, Integer Rows_, Integer Cols_,Integer NonZeroRow_=-1>
class Matrix: public TensorBase<T, std::make_integer_sequence<Integer, Rows_*Cols_>>
{
public:
static constexpr Integer Rows=Rows_;
static constexpr Integer Cols=Cols_;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_integer_sequence<Integer, Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }
inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}

inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}
// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}

inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}
};





template<Integer NonZeroRow_>
class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_integer_sequence<Integer,1>>
{
public:
static constexpr Integer Rows=1;
static constexpr Integer Cols=1;
using T= Real;
using type= Matrix<T,Rows,Cols>;
using subtype=T;
using MB = TensorBase<T, std::make_integer_sequence<Integer,Rows*Cols>>;
using MB::MB;
using MB::values;
static constexpr Integer NonZeroRow=NonZeroRow_;
inline constexpr static Integer rows() { return Rows; }
inline constexpr static Integer cols() { return Cols; }

inline constexpr std::array<T,Rows*Cols> &operator()()
{
return values;
}

inline constexpr const std::array<T,Rows*Cols> &operator()()const
{
return values;
}

inline constexpr T &operator[](const Integer i)
{
assert(i < Rows);
return values[i];
}

inline constexpr const T &operator[](const Integer i)const
{
assert(i < Rows);
return values[i];
}


// access matrix direclty by using I*Col+J index
inline constexpr T &operator()(const Integer i)
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr const T &operator()(const Integer i)const
{
assert(i < Rows*Cols);
return values[i];
}

inline constexpr T &operator()(const Integer i, const Integer j)
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}

inline constexpr const T &operator()(const Integer i, const Integer j) const
{
assert(i < Rows);
assert(j < Cols);
return values[i*cols() + j];
}


};

int main(int argc, char *argv[])
{
constexpr Matrix<Real,1,1> mat{1.0};


std::cout<<"it works"<<std::endl;
static_assert(mat(0,0)==1.0);



return 0;
}
以下是具体更改:
之前
template <typename T, Integer ... Is>
class TensorBase<T, std::index_sequence<Is...>>
template <typename T, Integer ... Is>
class TensorBase<T, std::integer_sequence<Integer, Is...>>

之前
template<typename T, Integer Rows_, Integer Cols_,Integer NonZeroRow_=-1>
class Matrix: public TensorBase<T, std::make_index_sequence<Rows_*Cols_>>
template<typename T, Integer Rows_, Integer Cols_,Integer NonZeroRow_=-1>
class Matrix: public TensorBase<T, std::make_integer_sequence<Integer, Rows_*Cols_>>

之前(在 Matrix中)
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB = TensorBase<T, std::make_integer_sequence<Integer, Rows*Cols>>;

之前
template<Integer NonZeroRow_>
class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_index_sequence<1>>
template<Integer NonZeroRow_>
class Matrix<Real,1,1,NonZeroRow_>: public TensorBase<Real, std::make_integer_sequence<Integer, 1>>

之前(在 Matrix<Real,1,1,NonZeroRow_>中)
using MB = TensorBase<T, std::make_index_sequence<Rows*Cols>>;
using MB = TensorBase<T, std::make_integer_sequence<Integer, Rows*Cols>>;

之前(在 Matrix<Real,1,1,NonZeroRow_>中)
inline constexpr T &operator()(const Integer i)const
inline constexpr const T &operator()(const Integer i)const

关于c++ - 编译可在Mac上运行,但不能在群集上运行(Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62679658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com