希望包含的C代码不会太长。我已经试着将它缩减到最低限度,以显示我遇到的问题。我使用异步I/O来写入少量数据。主代码调用aio_write,然后等待I/O回调设置全局变量,然后返回。我知道这不是通常的做法,但我正在尝试测试一些与更大的代码相关的东西,这些代码给我带来了真正的问题。
这段代码可以在Cray或GNU下编译和运行。
在英特尔下,它编译和运行良好,优化为"-O1“。但是具有更高优化级别的英特尔版本会在等待循环中挂起。回调确实设置了全局变量,但主代码从未看到它发生。
例如,在GNU下,输出是这样的:
> head -n 32 /dev/urandom > random_stuff.dat # put random data into test file
> ./write_only 10 # run the code; read and write ten doubles from the input file
reader read 5.991429e-16
writer wrote 80 bytes
writer received 5.991429e-16
writer thinks no_more_writes is 1
ran in 7.650000e-04 sec然而,对于英特尔(编译为"-O2“或更高),我得到
reader read 5.991429e-16
writer wrote 80 bytes
writer received 5.991429e-16
writer thinks no_more_writes is 1
main code still waiting
main code still waiting
main code still waiting
main code still waiting
....我对异步I/O比较陌生,我知道全局变量有一些问题,比如竞争条件。但是在这个简单的代码中应该不会出现这个问题。您知道问题出在哪里吗?为什么只针对优化过的英特尔版本才是错误的?(顺便说一句,如果我将全局变量wait替换为基于aio_error()的常见等待,那么所有编译器、所有优化级别都能正常工作。但是,我认为,对于我正在处理的更大、更真实的代码,我需要更多类似于我在这里所做的东西。)
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include<strings.h>
/* for EINPROGRESS value: */
#include <errno.h>
/* for timing */
#include <sys/time.h>
void writer(sigval_t sigval);
int chunksize, numelems;
double *alldata;
int no_more_writes=0;
int main(int argc, char **argv) {
struct aiocb aiowriter;
int fdin, fdout;
struct timeval start_time, end_time;
long int time_usec;
double time_sec;
char *infile = "random_stuff.dat";
if (argc < 2) {
printf("./write_only numelems\n");
return 1;
}
numelems = atoi(argv[1]);
chunksize = numelems * sizeof(double);
// allocate alldata
alldata = (double*)calloc(numelems, sizeof(double));
// open up input file and read
fdin = open(infile, O_RDONLY);
read(fdin, alldata, chunksize);
close(fdin);
printf("reader read %e \n", alldata[0]);
// writing
// open up output file
fdout = open("write_out.dat", O_WRONLY | O_CREAT, S_IRUSR | S_IRGRP);
// Check is opening of output file was successful:
if (fdout == -1) {
printf("cannot open 'write_out.dat'\n");
if (errno == EACCES || errno == EEXIST) {
printf("file aleady exists\n");
} else {
printf("errno is %d\n", errno);
}
return errno;
}
// bzero out aiowriter
bzero((char*)&aiowriter, sizeof(struct aiocb));
// set writer file desc. and other related info
aiowriter.aio_fildes = fdout;
aiowriter.aio_offset = 0;
aiowriter.aio_nbytes = chunksize;
// set writer buffer
aiowriter.aio_buf = (char*)alldata;
// set writer callback info
aiowriter.aio_sigevent.sigev_notify = SIGEV_THREAD;
aiowriter.aio_sigevent.sigev_notify_function = writer;
aiowriter.aio_sigevent.sigev_notify_attributes = NULL;
aiowriter.aio_sigevent.sigev_value.sival_ptr = &aiowriter;
// timing
gettimeofday(&start_time, NULL);
// the write
aio_write(&aiowriter);
int debugindex=0;
// wait for final writing to finish
while (no_more_writes != 1) {
usleep(100);
if (debugindex > 10000) {
printf("main code still waiting\n");
debugindex=1;
}
debugindex++;
}
// alternative wait loop:
//while (aio_error(&aiowriter) == EINPROGRESS) {}
// timing
gettimeofday(&end_time, NULL);
time_usec = 1000000*(end_time.tv_sec - start_time.tv_sec) +
(end_time.tv_usec - start_time.tv_usec);
time_sec = time_usec / 1000000.;
printf("ran in %e sec\n", time_sec);
free(alldata);
}
// write function
void writer(sigval_t sigval) {
int write_error;
// inputs: the AIO writing object
struct aiocb *writing;
writing = (struct aiocb *)sigval.sival_ptr;
// check amount of data written
write_error = aio_return(writing);
printf("writer wrote %d bytes\n", write_error);
printf("writer received %e\n", alldata[0]);
close(writing->aio_fildes);
no_more_writes = 1;
printf("writer thinks no_more_writes is %d\n",
no_more_writes);
return;
}发布于 2021-04-29 21:29:47
您需要将全局变量设置为volatile,否则编译器会优化冗余负载,因为它不会知道变量是从外部更改的。
volatile int no_more_writes = 0;https://stackoverflow.com/questions/67318090
复制相似问题