我刚接触 OpenMP,想知道这段代码有什么问题。它以串行方式工作。我正在使用 Ubuntu Linux 和 gfortran。
!
program test_rand
use omp_lib
implicit none
integer, parameter :: num_threads =36
integer*8,parameter :: nc = 1000000000
integer*8 ncirc,ncircs(0:35)
integer i,thread_num,istart,iend,ppt
real*8 x,y,dist,pi
integer,parameter :: seed = 864
call srand(seed)
do i=1,4
ncircs(i)=0
end do
ncirc=0
ppt=(nc+num_threads-1)/num_threads
istart=1
iend=nc
thread_num=1
!$ call omp_set_num_threads(num_threads)
!$omp parallel default(none) private(istart,iend,thread_num,i, &
!$omp dist,x,y,ncircs) shared(ppt,ncirc)
!$ thread_num = omp_get_thread_num()
!$ istart=thread_num*ppt+1
!$ iend = min(nc,thread_num*ppt+ppt)
print*,thread_num
do i=istart,iend
x=rand()
y=rand()
dist=sqrt((x-0.5)**2+(y-0.5)**2)
if (dist.le.0.5) ncircs(thread_num)=ncircs(thread_num)+1
end do
!$omp critical …Run Code Online (Sandbox Code Playgroud) I'm trying to generate dot product of two matrices using the below compiling command in Linux-GCC:
gcc -L / usr / lib64 / atlas -lblas code.c
这是似乎没有错误编译的代码:
#include < stdio.h>
int main()
{
int n,incx,incy;
int x[] = {1,2,3};
int y[] = {2,3,4};
int z;
incx =1;
incy =1;
n = 3;
z = sdot_(&n, x, &incx, y, &incy);
printf("%d", z);
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,我希望看到1 * 2 + 2 * 3 + 3 * 4 = 20,但是当我运行二进制文件时,将打印结果“ 3”。
有任何想法吗?
我一直在尝试以下代码:
program hello
write(*,"(i9)") 10
end program hello
Run Code Online (Sandbox Code Playgroud)
并改变格式字符串,尝试使写入输出的字符串大小恰好满足表示整数所需的大小,但到目前为止我无法管理它。如何在 Fortran 中编写“适合”整数?
我希望在Fortran中获得当前系统时间(以毫秒为单位).我无法使用system_clock,因为我无法弄清楚如何从中获取当前时间.
我试图将fortran 77代码复制到C#.这是引起麻烦的线(至少我认为):
real acl,c(0:10)
Run Code Online (Sandbox Code Playgroud)
如何c在C#中的某些方法下声明这个新列表?像这样:
float acl;
float[] c = new float[0:10];
Run Code Online (Sandbox Code Playgroud)
?
感谢您的答复.
使用并行性应该可以减少程序的时间,但这并没有发生在我身上.当我使用OpenMP并行编写代码时,运行时间会增加,即PARALLEL TIME> SERIAL TIME.
我的代码:
PROGRAM MAIN
use omp_lib
implicit none
REAL*8 Times1,Times2
INTEGER I,J
real, allocatable, dimension(:) :: a
allocate(a(1000))
DO J = 1, 1000
a(j)=j
ENDDO
! ***************NO PARALLEL CODE ************************************
call CPU_TIME(Times1)
write(*,*) 'CPU NO PARALLEL STARTED:',Times1
DO I = 1, 1000
DO J = 1, 500000
a(I)=a(I)+0.0001
end do
a(I)=a(I)+a(I)+a(I)
ENDDO
call CPU_TIME(Times2)
write(*,*) 'CPU CPU NO PARALLEL finished:',Times2
write(*,*) 'NO PARALLEL TIMES:',Times2-Times1
write(*,*) '---------------------------------------------------'
! ***************PARALLEL CODE ************************************
call CPU_TIME(Times1)
write(*,*) 'CPU PARALLEL STARTED:',Times1
!$OMP …Run Code Online (Sandbox Code Playgroud) 这个问题以前没有回答过.我正试图在Fortran中正确表示真实或任何数字.gfortran为我做的事情远没有.例如,当我声明变量REAL pi = 3.14159 fortran打印pi = 3.14159012而不是说3.14159000.见下文:
PROGRAM Test
IMPLICIT NONE
REAL:: pi = 3.14159
PRINT *, "PI = ",pi
END PROGRAM Test
Run Code Online (Sandbox Code Playgroud)
这打印:
PI = 3.14159012
Run Code Online (Sandbox Code Playgroud)
我可能已经预料到类似PI = 3.14159000的东西,因为REAL应该精确到至少8位小数.
如何在FORTRAN 90中编写一个程序来确定任何计算机中的机器epsilon?
type ErrorOrT[M[+_], A] = EitherT[M, Throwable, A]
type ErrorOr[A] = ErrorOrT[IO, A]
Run Code Online (Sandbox Code Playgroud) 所以我继承了用Fortran编写的这个(不是真正遗留的)项目.为了使它成为线程安全的,我必须将一个void*指针(称为user_data,你可能知道模式)传递给所有fortran例程,以便它们可以将它传递回回调(因此全局状态现在已正确分配堆).
令我真诚的惊讶的是,这导致了在最奇怪的地方完全崩溃和断块.毕竟,我只为所有函数添加了一个未更改的参数?
令我完全恐惧(我不是Fortran程序员,只是一个有解决问题诀窍的普通黑客),我了解到Fortran编译器只是忽略了第72列之外的所有内容,可能是因为列很昂贵或者其他东西,甚至没有发出警告(除了某些"类型错误"(Fortran中的haha类型 - 纪律,这是一个笑话)报道的情况).
直到今天,我一直在代码中找到遭受这种意外的意外后果的地方.
有没有可以在这种错误中可靠地检查Fortran代码库的工具?
并且,作为John Oliver的奖金问题:为什么72列限制仍然是一个问题?
我遇到了deallocate的问题并分配了部分FORTRAN代码的各个方面.特别是,我认为这个问题与通过搜索我在网络上的错误消息的内存分配有关.错误消息谈论无效指针,但是,我没有在我的程序中使用任何指针
在完成我的f循环的迭代#2(见下文)之后,程序崩溃了,或者大部分时间它崩溃了,有时它只是冻结了.我相信这就是bug的关键所在.因为程序运行到了这一点.
我没有显示子程序,但由于它们适用于其他模拟组合,我有理由相信它们不是问题.我正在使用deallocate并在程序中的其他地方分配(成功)所以我很惊讶它在这里不起作用.
我只是为了便于阅读而展示了该计划的一部分.特别是,我已经删除了对我编写的子程序的调用.我希望我已经为你的程序员提供了足够的信息来帮我解决问题.如果没有,请说明您想要的其他信息,我将很乐意遵守.我已经使用各种编译器选项编译了程序,并修复了一些错误并删除了任何警告.但是,此时,编译器选项不再向我提供任何信息.
allocate(poffvect(1:6))
allocate(phi1out(1:1))
allocate(phi2out(1:1))
allocate(phi1outs1(1:1))
allocate(phi2outs1(1:1))
dummy allocation
allocate(phi1outind(1:1))
allocate(phi2outind(1:1))
allocate(phi1outinds1(1:1))
allocate(phi2outinds1(1:1))
do e = 1, 6
print *,"e", e
do f = 1, 3
print *,"f", f, iteratst1(f), trim(filenumcharimp)
deallocate(phi1outinds1, STAT = AllocateStatus)
if (AllocateStatus /= 0) stop "Error during deallocation of phi1outinds1"
print *, "Allocatestatus of phi1outinds1 is", AllocateStatus
deallocate(phi2outinds1, STAT = AllocateStatus)
print *, "DeAllocatestatus of phi1outinds2 is", AllocateStatus
if (AllocateStatus /= 0) stop "Error during deallocation of phi2outinds1"
print *, "we deallocate f …Run Code Online (Sandbox Code Playgroud) 我遇到了以下情况:
POINTER(A,B(*)) ;INTEGER B
Run Code Online (Sandbox Code Playgroud)
什么是上述代码的C/C++等价物?
什么是;INTEGER B最后的?