为什么我不能删除 _mm_empty()?
Why can't I remove _mm_empty()?
我有一个带有一些 SSE2 指令的 C++ 函数。问题是我在使用 Microsoft Visual C++ 编译此代码时遇到以下链接器错误:
未解析的外部符号 _m_empty 在函数“void * __cdecl 中引用
处理(无效*)"
当我评论 _m_empty 时,我会收到运行时错误!
但它应该用于 MMX 指令,不是吗?
#include "mex.h"
#include <pthread.h>
#include <emmintrin.h>
#include <stdint.h>
#include <stdlib.h>
#define malloc_aligned(a,b) _aligned_malloc(a,b)
#define IS_ALIGNED(ptr) ((((uintptr_t)(ptr)) & 0xF) == 0)
#define NUM_FEATURES 32
#define __attribute__(A) /* do nothing */
/*
* This code is used for computing filter responses. It computes the
* response of a set of filters with a feature map.
*
* Multithreaded version.
*/
struct thread_data {
float *A;
float *B;
double *C;
mxArray *mxC;
const mwSize *A_dims;
const mwSize *B_dims;
mwSize C_dims[2];
};
// convolve A and B
void *process(void *thread_arg) {
thread_data *args = (thread_data *)thread_arg;
float *A = args->A;
float *B = args->B;
double *C = args->C;
const mwSize *A_dims = args->A_dims;
const mwSize *B_dims = args->B_dims;
const mwSize *C_dims = args->C_dims;
__m128 a,b,c;
double *dst = C;
for (int x = 0; x < C_dims[1]; x++) {
for (int y = 0; y < C_dims[0]; y++) {
__m128 v = _mm_setzero_ps();
const float *A_src = A + y*NUM_FEATURES + x*A_dims[0]*NUM_FEATURES;
const float *B_src = B;
for (int xp = 0; xp < B_dims[1]; xp++) {
const float *A_off = A_src;
const float *B_off = B_src;
for (int yp = 0; yp < B_dims[0]; yp++) {
a = _mm_load_ps(A_off+0);
b = _mm_load_ps(B_off+0);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+4);
b = _mm_load_ps(B_off+4);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+8);
b = _mm_load_ps(B_off+8);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+12);
b = _mm_load_ps(B_off+12);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+16);
b = _mm_load_ps(B_off+16);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+20);
b = _mm_load_ps(B_off+20);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+24);
b = _mm_load_ps(B_off+24);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+28);
b = _mm_load_ps(B_off+28);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
// N.B. Unroll me more/less if you change NUM_FEATURES
A_off += NUM_FEATURES;
B_off += NUM_FEATURES;
}
A_src += A_dims[0]*NUM_FEATURES;
B_src += B_dims[0]*NUM_FEATURES;
}
// buf[] must be 16-byte aligned
float buf[4] __attribute__ ((aligned (16)));
_mm_store_ps(buf, v);
_mm_empty();
*(dst++) = buf[0]+buf[1]+buf[2]+buf[3];
}
}
pthread_exit(NULL);
return 0;
}
float *prepare(float *in, const int *dims) {
float *F = (float *)malloc_aligned(16, dims[0]*dims[1]*NUM_FEATURES*sizeof(float));
// Sanity check that memory is aligned
if (!IS_ALIGNED(F))
mexErrMsgTxt("Memory not aligned");
float *p = F;
for (int x = 0; x < dims[1]; x++) {
for (int y = 0; y < dims[0]; y++) {
for (int f = 0; f < dims[2]; f++)
*(p++) = in[y + f*dims[0]*dims[1] + x*dims[0]];
for (int f = dims[2]; f < NUM_FEATURES; f++)
*(p++) = 0;
}
}
return F;
}
// matlab entry point
// C = fconv(A, cell of B, start, end);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 4)
mexErrMsgTxt("Wrong number of inputs");
if (nlhs != 1)
mexErrMsgTxt("Wrong number of outputs");
// get A
const mxArray *mxA = prhs[0];
if (mxGetNumberOfDimensions(mxA) != 3 ||
mxGetClassID(mxA) != mxSINGLE_CLASS)
mexErrMsgTxt("Invalid input: A");
// get B and start/end
const mxArray *cellB = prhs[1];
mwSize num_bs = mxGetNumberOfElements(cellB);
int start = (int)mxGetScalar(prhs[2]) - 1;
int end = (int)mxGetScalar(prhs[3]) - 1;
if (start < 0 || end >= num_bs || start > end)
mexErrMsgTxt("Invalid input: start/end");
int len = end-start+1;
// start threads
thread_data *td = (thread_data *)mxCalloc(len, sizeof(thread_data));
pthread_t *ts = (pthread_t *)mxCalloc(len, sizeof(pthread_t));
const mwSize *A_dims = mxGetDimensions(mxA);
float *A = prepare((float *)mxGetPr(mxA), A_dims);
for (int i = 0; i < len; i++) {
const mxArray *mxB = mxGetCell(cellB, i+start);
td[i].A_dims = A_dims;
td[i].A = A;
td[i].B_dims = mxGetDimensions(mxB);
td[i].B = prepare((float *)mxGetPr(mxB), td[i].B_dims);
if (mxGetNumberOfDimensions(mxB) != 3 ||
mxGetClassID(mxB) != mxSINGLE_CLASS ||
td[i].A_dims[2] != td[i].B_dims[2])
mexErrMsgTxt("Invalid input: B");
// compute size of output
int height = td[i].A_dims[0] - td[i].B_dims[0] + 1;
int width = td[i].A_dims[1] - td[i].B_dims[1] + 1;
if (height < 1 || width < 1)
mexErrMsgTxt("Invalid input: B should be smaller than A");
td[i].C_dims[0] = height;
td[i].C_dims[1] = width;
td[i].mxC = mxCreateNumericArray(2, td[i].C_dims, mxDOUBLE_CLASS, mxREAL);
td[i].C = (double *)mxGetPr(td[i].mxC);
if (pthread_create(&ts[i], NULL, process, (void *)&td[i]))
mexErrMsgTxt("Error creating thread");
}
// wait for the treads to finish and set return values
void *status;
plhs[0] = mxCreateCellMatrix(1, len);
for (int i = 0; i < len; i++) {
pthread_join(ts[i], &status);
mxSetCell(plhs[0], i, td[i].mxC);
free(td[i].B);
}
mxFree(td);
mxFree(ts);
free(A);
}
根据 this link,MMX 未针对 x64 实现。使用成熟的 SSE2 n x64。
我认为您代码中的 _mm_empty
变成了对 _m_empty
的引用,因为 they're synonyms,并且您的构建环境仍然有一些 header 和 [=13] =] 之类的。
但奇怪的是,您的构建环境实际上并未提供内在函数的定义。是否有关于它被隐式声明的编译器警告?这很奇怪,因为我预计完全缺乏 MMX 支持意味着 _mm_empty
/ _m_empty
等价物也不会存在。
运行时错误可能与此无关。就像 Paul R 在评论中指出的那样,您假设如果可以编译未修改的源代码,它就可以工作。情况可能并非如此,因为 _mm_empty
对评论过此问题的 x86 asm 专家(包括我)来说似乎没有必要。
我认为 Paul R 猜测您可能在某处有一个未对齐的指针听起来很合理。这些数组中的任何一个的对齐方式是否取决于 sizeof
指针?如果 struct thread_data
类似于:
struct thread_data {
some_type *ptr1;
some_type *ptr2;
int a;
int b;
float A[1024];
float B[1024];
...
};
然后 32 位构建将具有 16B 对齐的数组,但 64 位构建不会。
所以调试你的运行时错误,找出它是什么。 如果您只告诉我们 "runtime error"。 如果您一开始就告诉我们它是什么,我们可以告诉您一种方式或其他是否可能与删除 _mm_empty
.
有关
感谢 harold、Paul 和 Peter,我找到了问题所在!你是对的,运行时错误与 _mm_empty 无关!问题是 _aligned_malloc 输入参数顺序。当我交换输入时,运行时错误消失了。
另一个错误是 free() 函数。 _aligned_free() 必须用于释放对齐内存。
正如 harold 所建议的,我将主循环更改为使用三个独立的 v。如果我做错了,请纠正我。现在程序(不是函数!)运行速度快了 300 毫秒(2.4 秒 -> 2.1 秒)。
void *process(void *thread_arg) {
thread_data *args = (thread_data *)thread_arg;
float *A = args->A;
float *B = args->B;
double *C = args->C;
const mwSize *A_dims = args->A_dims;
const mwSize *B_dims = args->B_dims;
const mwSize *C_dims = args->C_dims;
__m128 a,b,c;
double *dst = C;
for (int x = 0; x < C_dims[1]; x++) {
for (int y = 0; y < C_dims[0]; y++) {
__m128 v = _mm_setzero_ps(), v1 = _mm_setzero_ps(), v2 = _mm_setzero_ps(), v3 = _mm_setzero_ps();
const float *A_src = A + y*NUM_FEATURES + x*A_dims[0]*NUM_FEATURES;
const float *B_src = B;
for (int xp = 0; xp < B_dims[1]; xp++) {
const float *A_off = A_src;
const float *B_off = B_src;
for (int yp = 0; yp < B_dims[0]; yp++) {
a = _mm_load_ps(A_off+0);
b = _mm_load_ps(B_off+0);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+4);
b = _mm_load_ps(B_off+4);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
a = _mm_load_ps(A_off+8);
b = _mm_load_ps(B_off+8);
c = _mm_mul_ps(a, b);
v3 = _mm_add_ps(v3, c);
a = _mm_load_ps(A_off+12);
b = _mm_load_ps(B_off+12);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+16);
b = _mm_load_ps(B_off+16);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
a = _mm_load_ps(A_off+20);
b = _mm_load_ps(B_off+20);
c = _mm_mul_ps(a, b);
v3 = _mm_add_ps(v3, c);
a = _mm_load_ps(A_off+24);
b = _mm_load_ps(B_off+24);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+28);
b = _mm_load_ps(B_off+28);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
// N.B. Unroll me more/less if you change NUM_FEATURES
A_off += NUM_FEATURES;
B_off += NUM_FEATURES;
}
A_src += A_dims[0]*NUM_FEATURES;
B_src += B_dims[0]*NUM_FEATURES;
}
v = _mm_add_ps(v, v1);
v = _mm_add_ps(v, v2);
v = _mm_add_ps(v, v3);
// buf[] must be 16-byte aligned
__declspec(align(16)) float buf[4];
_mm_store_ps(buf, v);
*(dst++) = buf[0]+buf[1]+buf[2]+buf[3];
}
}
pthread_exit(NULL);
return 0;
}
我有一个带有一些 SSE2 指令的 C++ 函数。问题是我在使用 Microsoft Visual C++ 编译此代码时遇到以下链接器错误:
未解析的外部符号 _m_empty 在函数“void * __cdecl 中引用 处理(无效*)"
当我评论 _m_empty 时,我会收到运行时错误! 但它应该用于 MMX 指令,不是吗?
#include "mex.h"
#include <pthread.h>
#include <emmintrin.h>
#include <stdint.h>
#include <stdlib.h>
#define malloc_aligned(a,b) _aligned_malloc(a,b)
#define IS_ALIGNED(ptr) ((((uintptr_t)(ptr)) & 0xF) == 0)
#define NUM_FEATURES 32
#define __attribute__(A) /* do nothing */
/*
* This code is used for computing filter responses. It computes the
* response of a set of filters with a feature map.
*
* Multithreaded version.
*/
struct thread_data {
float *A;
float *B;
double *C;
mxArray *mxC;
const mwSize *A_dims;
const mwSize *B_dims;
mwSize C_dims[2];
};
// convolve A and B
void *process(void *thread_arg) {
thread_data *args = (thread_data *)thread_arg;
float *A = args->A;
float *B = args->B;
double *C = args->C;
const mwSize *A_dims = args->A_dims;
const mwSize *B_dims = args->B_dims;
const mwSize *C_dims = args->C_dims;
__m128 a,b,c;
double *dst = C;
for (int x = 0; x < C_dims[1]; x++) {
for (int y = 0; y < C_dims[0]; y++) {
__m128 v = _mm_setzero_ps();
const float *A_src = A + y*NUM_FEATURES + x*A_dims[0]*NUM_FEATURES;
const float *B_src = B;
for (int xp = 0; xp < B_dims[1]; xp++) {
const float *A_off = A_src;
const float *B_off = B_src;
for (int yp = 0; yp < B_dims[0]; yp++) {
a = _mm_load_ps(A_off+0);
b = _mm_load_ps(B_off+0);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+4);
b = _mm_load_ps(B_off+4);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+8);
b = _mm_load_ps(B_off+8);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+12);
b = _mm_load_ps(B_off+12);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+16);
b = _mm_load_ps(B_off+16);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+20);
b = _mm_load_ps(B_off+20);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+24);
b = _mm_load_ps(B_off+24);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
a = _mm_load_ps(A_off+28);
b = _mm_load_ps(B_off+28);
c = _mm_mul_ps(a, b);
v = _mm_add_ps(v, c);
// N.B. Unroll me more/less if you change NUM_FEATURES
A_off += NUM_FEATURES;
B_off += NUM_FEATURES;
}
A_src += A_dims[0]*NUM_FEATURES;
B_src += B_dims[0]*NUM_FEATURES;
}
// buf[] must be 16-byte aligned
float buf[4] __attribute__ ((aligned (16)));
_mm_store_ps(buf, v);
_mm_empty();
*(dst++) = buf[0]+buf[1]+buf[2]+buf[3];
}
}
pthread_exit(NULL);
return 0;
}
float *prepare(float *in, const int *dims) {
float *F = (float *)malloc_aligned(16, dims[0]*dims[1]*NUM_FEATURES*sizeof(float));
// Sanity check that memory is aligned
if (!IS_ALIGNED(F))
mexErrMsgTxt("Memory not aligned");
float *p = F;
for (int x = 0; x < dims[1]; x++) {
for (int y = 0; y < dims[0]; y++) {
for (int f = 0; f < dims[2]; f++)
*(p++) = in[y + f*dims[0]*dims[1] + x*dims[0]];
for (int f = dims[2]; f < NUM_FEATURES; f++)
*(p++) = 0;
}
}
return F;
}
// matlab entry point
// C = fconv(A, cell of B, start, end);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 4)
mexErrMsgTxt("Wrong number of inputs");
if (nlhs != 1)
mexErrMsgTxt("Wrong number of outputs");
// get A
const mxArray *mxA = prhs[0];
if (mxGetNumberOfDimensions(mxA) != 3 ||
mxGetClassID(mxA) != mxSINGLE_CLASS)
mexErrMsgTxt("Invalid input: A");
// get B and start/end
const mxArray *cellB = prhs[1];
mwSize num_bs = mxGetNumberOfElements(cellB);
int start = (int)mxGetScalar(prhs[2]) - 1;
int end = (int)mxGetScalar(prhs[3]) - 1;
if (start < 0 || end >= num_bs || start > end)
mexErrMsgTxt("Invalid input: start/end");
int len = end-start+1;
// start threads
thread_data *td = (thread_data *)mxCalloc(len, sizeof(thread_data));
pthread_t *ts = (pthread_t *)mxCalloc(len, sizeof(pthread_t));
const mwSize *A_dims = mxGetDimensions(mxA);
float *A = prepare((float *)mxGetPr(mxA), A_dims);
for (int i = 0; i < len; i++) {
const mxArray *mxB = mxGetCell(cellB, i+start);
td[i].A_dims = A_dims;
td[i].A = A;
td[i].B_dims = mxGetDimensions(mxB);
td[i].B = prepare((float *)mxGetPr(mxB), td[i].B_dims);
if (mxGetNumberOfDimensions(mxB) != 3 ||
mxGetClassID(mxB) != mxSINGLE_CLASS ||
td[i].A_dims[2] != td[i].B_dims[2])
mexErrMsgTxt("Invalid input: B");
// compute size of output
int height = td[i].A_dims[0] - td[i].B_dims[0] + 1;
int width = td[i].A_dims[1] - td[i].B_dims[1] + 1;
if (height < 1 || width < 1)
mexErrMsgTxt("Invalid input: B should be smaller than A");
td[i].C_dims[0] = height;
td[i].C_dims[1] = width;
td[i].mxC = mxCreateNumericArray(2, td[i].C_dims, mxDOUBLE_CLASS, mxREAL);
td[i].C = (double *)mxGetPr(td[i].mxC);
if (pthread_create(&ts[i], NULL, process, (void *)&td[i]))
mexErrMsgTxt("Error creating thread");
}
// wait for the treads to finish and set return values
void *status;
plhs[0] = mxCreateCellMatrix(1, len);
for (int i = 0; i < len; i++) {
pthread_join(ts[i], &status);
mxSetCell(plhs[0], i, td[i].mxC);
free(td[i].B);
}
mxFree(td);
mxFree(ts);
free(A);
}
根据 this link,MMX 未针对 x64 实现。使用成熟的 SSE2 n x64。
我认为您代码中的 _mm_empty
变成了对 _m_empty
的引用,因为 they're synonyms,并且您的构建环境仍然有一些 header 和 [=13] =] 之类的。
但奇怪的是,您的构建环境实际上并未提供内在函数的定义。是否有关于它被隐式声明的编译器警告?这很奇怪,因为我预计完全缺乏 MMX 支持意味着 _mm_empty
/ _m_empty
等价物也不会存在。
运行时错误可能与此无关。就像 Paul R 在评论中指出的那样,您假设如果可以编译未修改的源代码,它就可以工作。情况可能并非如此,因为 _mm_empty
对评论过此问题的 x86 asm 专家(包括我)来说似乎没有必要。
我认为 Paul R 猜测您可能在某处有一个未对齐的指针听起来很合理。这些数组中的任何一个的对齐方式是否取决于 sizeof
指针?如果 struct thread_data
类似于:
struct thread_data {
some_type *ptr1;
some_type *ptr2;
int a;
int b;
float A[1024];
float B[1024];
...
};
然后 32 位构建将具有 16B 对齐的数组,但 64 位构建不会。
所以调试你的运行时错误,找出它是什么。 如果您只告诉我们 "runtime error"。 如果您一开始就告诉我们它是什么,我们可以告诉您一种方式或其他是否可能与删除 _mm_empty
.
感谢 harold、Paul 和 Peter,我找到了问题所在!你是对的,运行时错误与 _mm_empty 无关!问题是 _aligned_malloc 输入参数顺序。当我交换输入时,运行时错误消失了。
另一个错误是 free() 函数。 _aligned_free() 必须用于释放对齐内存。
正如 harold 所建议的,我将主循环更改为使用三个独立的 v。如果我做错了,请纠正我。现在程序(不是函数!)运行速度快了 300 毫秒(2.4 秒 -> 2.1 秒)。
void *process(void *thread_arg) {
thread_data *args = (thread_data *)thread_arg;
float *A = args->A;
float *B = args->B;
double *C = args->C;
const mwSize *A_dims = args->A_dims;
const mwSize *B_dims = args->B_dims;
const mwSize *C_dims = args->C_dims;
__m128 a,b,c;
double *dst = C;
for (int x = 0; x < C_dims[1]; x++) {
for (int y = 0; y < C_dims[0]; y++) {
__m128 v = _mm_setzero_ps(), v1 = _mm_setzero_ps(), v2 = _mm_setzero_ps(), v3 = _mm_setzero_ps();
const float *A_src = A + y*NUM_FEATURES + x*A_dims[0]*NUM_FEATURES;
const float *B_src = B;
for (int xp = 0; xp < B_dims[1]; xp++) {
const float *A_off = A_src;
const float *B_off = B_src;
for (int yp = 0; yp < B_dims[0]; yp++) {
a = _mm_load_ps(A_off+0);
b = _mm_load_ps(B_off+0);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+4);
b = _mm_load_ps(B_off+4);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
a = _mm_load_ps(A_off+8);
b = _mm_load_ps(B_off+8);
c = _mm_mul_ps(a, b);
v3 = _mm_add_ps(v3, c);
a = _mm_load_ps(A_off+12);
b = _mm_load_ps(B_off+12);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+16);
b = _mm_load_ps(B_off+16);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
a = _mm_load_ps(A_off+20);
b = _mm_load_ps(B_off+20);
c = _mm_mul_ps(a, b);
v3 = _mm_add_ps(v3, c);
a = _mm_load_ps(A_off+24);
b = _mm_load_ps(B_off+24);
c = _mm_mul_ps(a, b);
v1 = _mm_add_ps(v1, c);
a = _mm_load_ps(A_off+28);
b = _mm_load_ps(B_off+28);
c = _mm_mul_ps(a, b);
v2 = _mm_add_ps(v2, c);
// N.B. Unroll me more/less if you change NUM_FEATURES
A_off += NUM_FEATURES;
B_off += NUM_FEATURES;
}
A_src += A_dims[0]*NUM_FEATURES;
B_src += B_dims[0]*NUM_FEATURES;
}
v = _mm_add_ps(v, v1);
v = _mm_add_ps(v, v2);
v = _mm_add_ps(v, v3);
// buf[] must be 16-byte aligned
__declspec(align(16)) float buf[4];
_mm_store_ps(buf, v);
*(dst++) = buf[0]+buf[1]+buf[2]+buf[3];
}
}
pthread_exit(NULL);
return 0;
}