C++ 使用对齐数据卸载到intel麦克风

C++ 使用对齐数据卸载到intel麦克风,c++,alignment,xeon-phi,intel-mic,offloading,C++,Alignment,Xeon Phi,Intel Mic,Offloading,当我编译下面的代码时,我得到了警告: src/parallel_hashing.cpp(50): warning #3218: *MIC* class/struct may fail when offloaded because this field is misaligned or contains data that is misaligned __declspec(align(64)) list<HashEntry> mChains[TABLE_SIZE];

当我编译下面的代码时,我得到了警告:

src/parallel_hashing.cpp(50): warning #3218: *MIC* class/struct may fail when offloaded because this field is misaligned or contains data that is misaligned    __declspec(align(64)) list<HashEntry> mChains[TABLE_SIZE];
                                                                                                                                                                                                ^ 
src/parallel_hashing.cpp(50):警告#3218:*MIC*类/结构在卸载时可能会失败,因为此字段未对齐或包含未对齐的数据_declspec(align(64))list mChains[表大小];
^ 
尽管我认为我已经注意到了所有需要的对齐。 当我执行它时,我得到一个分段错误

有人知道我该怎么做吗

守则:

#pragma offload_attribute(push, target(mic))
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <list>
#include <queue>
#define TABLE_SIZE 3019
#define NUM_OF_THREADS 60
#define HASHINH_PRIME 2654435761

using namespace std;

int Hash(int key)
{
    return (HASHINH_PRIME * key) % TABLE_SIZE;
}

class __declspec(align(64)) HashEntry
{
private:

    unsigned int mKey;
    int mDummy[5];

public:

    //Ctor
    HashEntry(unsigned int key)
    {
        mKey = key;
        for (int i = 0; i < 5; i++)
        {
            mDummy[i] = key + i;
        }
    }

    unsigned int GetKey()
    {
        return mKey; 
    }
};

class HashTable
{
private:
    unsigned int mMaxChainLength;
    __declspec(align(64)) list<HashEntry> mChains[TABLE_SIZE];

    void UpdateMaxChainLength()
    {
        int newMaxChainLength = 0;
        for (int i = 0; i < TABLE_SIZE; i++)
        {
            int currentLength = mChains[i].size();

            if (currentLength > newMaxChainLength)
            {
                newMaxChainLength = currentLength;
            }
        }

        mMaxChainLength = newMaxChainLength;
    }

    list<HashEntry>* GetChain(unsigned int key)
    {
        return &mChains[Hash(key)];
    }

public:

    HashTable()
    {
        mMaxChainLength = 0;
    }

    unsigned int GetMaxChainLength()
    {
        return mMaxChainLength;
    }

    void InsertKey(unsigned int key)
    {
        //create a new entry with the key
        HashEntry newEntry = HashEntry(key);

        //get the specific chain
        list<HashEntry>* chain = GetChain(key);

        //insert the new entry to the chain
        chain->insert(chain->end(), newEntry);

        //update mMaxChainLength if needed
        if (chain->size() > mMaxChainLength)
            mMaxChainLength = chain->size();
    }

    void DeleteKey(unsigned int key)
    {
        //get the specific chain
        list<HashEntry>* chain = GetChain(key);

        list<HashEntry>::iterator it = chain->begin();
        while (it != chain->end() && it->GetKey() != key)
        {
            it++;
        }

        if (it != chain->end())
        {
            chain->erase(it);

            //update mMaxChainLength if the chain (before delete of key) was in the size of mMaxChainLength
            if (chain->size() == mMaxChainLength - 1)
                UpdateMaxChainLength();
        }
    }

    bool SearchKey(unsigned int key)
    {
        //get the specific chain
        list<HashEntry>* chain = GetChain(key);

        list<HashEntry>::iterator it = chain->begin();
        while (it != chain->end() && it->GetKey() != key)
        {
            it++;
        }

        return (it != chain->end());
    }
};

HashTable mCoreTables[NUM_OF_THREADS]; 

#pragma offload_attribute(pop)

void CreateTables()
{
    //parallelize via OpenMP on MIC
    #pragma offload target(mic)
    #pragma omp parallel num_threads(NUM_OF_THREADS)
    {
        int core = omp_get_thread_num();
        for (int i=0; i<100; i++)
        {
            mCoreTables[core].InsertKey(i); //for example
        }
    }   
}

int main()
{
    omp_set_nested(1);
    omp_set_max_active_levels(2);
    omp_set_num_threads(NUM_OF_THREADS);

    CreateTables(); 
}
#杂注卸载_属性(推送、目标(麦克风))
#包括
#包括
#包括
#包括
#包括
#定义表格大小3019
#定义线程的数量60
#定义HASHINH_素数2654435761
使用名称空间std;
整数散列(整数键)
{
返回(哈希素数*键)%表大小;
}
类uu declspec(align(64))哈希项
{
私人:
无符号整数mKey;
国际货币基金组织[5];
公众:
//执行器
HashEntry(无符号整数键)
{
mKey=键;
对于(int i=0;i<5;i++)
{
mDummy[i]=键+i;
}
}
unsigned int GetKey()
{
返回mKey;
}
};
类哈希表
{
私人:
无符号整数mm长;
__declspec(align(64))列出mChains[表大小];
void UpdateMaxChainLength()
{
int newMaxChainLength=0;
对于(int i=0;inewMaxChainLength)
{
newMaxChainLength=当前长度;
}
}
mMaxChainLength=newMaxChainLength;
}
列表*GetChain(未签名的整数键)
{
返回&mChains[Hash(key)];
}
公众:
哈希表()
{
mMaxChainLength=0;
}
unsigned int GetMaxChainLength()
{
返回mMaxChainLength;
}
void InsertKey(无符号int键)
{
//使用该键创建一个新条目
HashEntry newEntry=HashEntry(键);
//获取特定的链
list*chain=GetChain(键);
//将新条目插入到链中
链->插入(链->结束(),新条目);
//如果需要,更新mMaxChainLength
如果(链->大小()>mMaxChainLength)
mMaxChainLength=链->尺寸();
}
void DeleteKey(无符号整数键)
{
//获取特定的链
list*chain=GetChain(键);
列表::迭代器it=chain->begin();
while(it!=chain->end()&&it->GetKey()!=key)
{
it++;
}
如果(it!=链->结束())
{
链->擦除(it);
//如果链(在删除键之前)的大小为mMaxChainLength,则更新mMaxChainLength
如果(链->大小()==mMaxChainLength-1)
UpdateMaxChainLength();
}
}
布尔搜索键(无符号整数键)
{
//获取特定的链
list*chain=GetChain(键);
列表::迭代器it=chain->begin();
while(it!=chain->end()&&it->GetKey()!=key)
{
it++;
}
返回(it!=chain->end());
}
};
哈希表mCoreTables[线程数];
#pragma卸载_属性(pop)
void CreateTables()
{
//通过麦克风上的OpenMP并行化
#pragma卸载目标(mic)
#pragma omp并行线程数(线程数)
{
int core=omp_get_thread_num();

对于(inti=0;i您提出了一个稍微宽泛的问题:如何对齐STL容器中的数据。

<强>回答< /强>:考虑使用

boost::alignment::aligned_allocator
..以对齐数据成员。也不要忘记-DBOOST\u align

阅读更多(第10-13页提供了关于你问题的更多细节)