简介
此文简单介绍如何使用intel c++编译器实现向量化加速。
全文如下安排:
- base : 待优化的源代码。
- vectorization : 第一个向量化版本。
- aligned : 内存对其对向量化的影响。
base
base版本代码:
// filename : main.cpp
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <cstdint>
#include <malloc.h>
#include <windows.h>
using namespace std;
int64_t cpu_freq;
int64_t cpu_counter(){
int64_t clock;
QueryPerformanceCounter((LARGE_INTEGER*)&clock);
return clock;
}
// output time
#if 1
int64_t gloabel_timer_begin;
int64_t gloabel_timer_end;
#define TB__ gloabel_timer_begin=cpu_counter()
#define TE__ gloabel_timer_end =cpu_counter(); \
cout << __LINE__ << " : " << double(gloabel_timer_end-gloabel_timer_begin)/double(cpu_freq) << " seconds" << endl
#else
#define TB__
#define TE__
#endif
// repeat times
#define REPEATTIMES 100000
// initialize data
void init(float *data, int rows, int cols, int true_cols){
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
data[i*true_cols+j] = float(rand())/float(RAND_MAX);
}
}
}
void multiply(float *C, float *A, float *B, int rows, int cols, int true_cols);
void print_sum(float *data, int rows, int cols, int true_cols){
float total = 0