我是 AVX 编程的新手。我想加载一个__m256
带有 16 个短整数或 16 位值的向量,但我无法这样做。
这是我的尝试。它给出了以下错误:
使用类型 'int' 初始化类型 '__m256' 时类型不兼容 __m256 result = _mm256_load_epi16((__m256*)&int_array);
#include <stdio.h>
#include <stdint.h>
#include <immintrin.h>
int main() {
int i;
short int int_array[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
__m256 result = _mm256_load_epi16((__m256*)&int_array);
short int* res = (short int*)&result;
printf("%d %d %d %d %d %d %d %d\n", res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在C中实现一个数学库,该数学库大量使用乘法。最初,我所有的乘法都是使用进行的uint16_t
。最近,我将其中的许多更改为,uint32_t
然后发现我的代码运行时间几乎翻了一番。正如我在Intel x64处理器中所认为的那样,我感到困惑,32位和16位乘法需要相同的时钟周期。我写了诊断代码,请在下面找到
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "cpucycles.c"
#define REPEAT 10000
#define OUT_REPEAT 100000
void main(){
uint16_t a_16[REPEAT], b_16[REPEAT], c_16[REPEAT];
uint32_t a_32[REPEAT], b_32[REPEAT], c_32[REPEAT];
int32_t i,j;
uint64_t clock1, clock2, CLOCK16, CLOCK32;
uint64_t acc=0;
time_t t;
srand((unsigned) time(&t));
clock1=clock2=CLOCK16=CLOCK32=0;
for(j=0;j<OUT_REPEAT;j++){
for(i=0;i<REPEAT;i++){
a_16[i]=rand()& ( (1<<13) -1); //need 13-bit integers only
b_16[i]=rand()& ( (1<<13) -1);
a_32[i]=rand()&( (1<<19) -1);
b_32[i]=rand()&( (1<<19) -1); //need 19-bit integers only
}
clock1=cpucycles();
for(i=0;i<REPEAT;i++){
c_16[i]=a_16[i]*b_16[i];
}
clock2=cpucycles();
CLOCK16=CLOCK16+(clock2-clock1);
clock1=cpucycles();
for(i=0;i<REPEAT;i++){
c_32[i]=a_32[i]*b_32[i]; …
Run Code Online (Sandbox Code Playgroud) 我最近开始使用CUDA开始使用GPU.作为入门程序,我试图有效地实现简单的矩阵乘法
C = AB
,从朴素矩阵乘法开始(每个线程加载C中元素的A和B的所有元素),平铺实现(线程协同加载来自共享内存中的块中A和B的元素块以减少全局内存交通)提供良好的加速.但是,在平铺实现中,对全局内存的访问也不是合并的顺序.因此,为了提高性能,最好转置矩阵B然后相乘.以下是我的代码,
#include<stdio.h>
#include<stdlib.h>
#include<cuda_runtime.h>
#include <time.h>
#include <sys/time.h>
void querydeviceprop();
void allocate_matrix(float *h_a, float *h_b, int matDim);
void verify(float *h_c, float *h_c_check, int matDim);
void print_matrix(float *ha, int m,int n);
void transpose_matrix(float *ha, int matDim);
void mat_mul();
#define TILE_WIDTH 16 //should be equal to numThread for tiling implementation
__global__ void MatrixMult_tiling(float *d_a,float *d_b,float *d_c, int dim){
__shared__ float ta[TILE_WIDTH][TILE_WIDTH]; //to load one tile of A
__shared__ float tb[TILE_WIDTH][TILE_WIDTH]; //to load one tile of A
int bx,by,tx,ty,i,j; …
Run Code Online (Sandbox Code Playgroud)