# 通过英特尔 Cilk Plus 数组符号实现外层循环向量化

C++ 数组符号可以通过模拟外层循环向量化来阻止循环（对外层循环进行向量化处理，同时保留内层循环的结构）。下面的示例基于德布洛特集合的计算：

```void serial_mandel(
float c_re[SIZE], float c_im[SIZE],
unsigned int max_recurrences,
char output[SIZE])
{
for (int i = 0; i < SIZE; i++) {
unsigned int iter = 0;
float z_re = c_re[i];
float z_im = c_im[i];

// If the loop reaches max_recurrences, c is an element of
// the Mandelbrot set.
while (iter < max_recurrences) {
if (z_re * z_re + z_im * z_im > 4.0) {
break; // escape from a circle of radius 2
}

float new_z_re = c_re[i] + z_re*z_re – z_im*z_im;
float new_z_im = c_im[i] + 2.*z_re*z_im;
iter++;
}

// Scale the number of iterations to the range of an 8-bpp image
output = static_cast<unsigned char>(static_cast<float>(iter) /
max_recurrences * 255);
}
}
```

```void mandel_v(
double c_re[SIZE], double c_im[SIZE],
int max_recur,
char output[SIZE])
{
float count[VLEN];
for (int i = 0; i < size; i += VLEN) {
double z_re[VLEN], z_im[VLEN], new_z_re[VLEN], new_z_im[VLEN];
int test[VLEN];
z_re[:] = c_re[i:VLEN];
z_im[:] = c_im[i:VLEN];
int iter;

// Count how many times you can multiply z by itself
// until it is greater than 4. count[:] is the result.
count[:] = 0;
for (iter=0; iter<max_recur; iter++) {
test[:] = (z_re[:] * z_re[:] + z_im[:] * z_im[:]) <= 4.;
if (__sec_reduce_all_zero(test[:])) {
break;>
}
count[:] += test[:];

// add 1 or 0 depending on whether z[:] is <= 4
new_z_re[:] = c_re[:] + z_re[:]*z_re[:] - z_im[:]*z_im[:];
new_z_im[:] = c_im[:] + 2.*z_re[:]*z_im[:];
z_re[:] = new_z_re[:];
z_im[:] = new_z_im[:];
}
output[i:VLEN] = (char)((float)count[:] / max_recur*255);
}
}
```

ISCA 2012 论文:《传统编程是否能缩小并行计算应用的 Ninja 性能差距？》（2012 年 6 月）

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.