OpenMP并行GSL常微分方程的计算

时间:2019-06-09 08:05:20

标签: c openmp gsl

我正在尝试并行化我的代码,但是出现错误。我需要计算一个柯西问题(已经完成),但是我需要使用OpenMP lib对其进行并行化处理。

我试图用OpenMP编写一些代码,但是它不起作用。

我创建了一个结构来收集结果。

struct Dots {
    double par;
    double x;
    double y;
};

这是我带有参数的目标函数。

int ode_func (double x, const double y[], double f[], void *params)
{

    double mu = *(int *)params;
    f[0] = x + 2 * y[0] / (1 + mu * mu);
    return GSL_SUCCESS;
}

这是主要功能。我目前没有找到一种方法来创建结构数组的数组,但这不是主要问题。

void calc_cauchy_problem(struct Dots ArrayOfDots[], double x_start, double x_end, double y_start,
        int count) {

    int dim = 1;
    double x = x_start;
    double y[1] = {y_start};
    int mu = 5;
    int param = 0;
    gsl_odeiv2_system sys = {ode_func, NULL, dim, &param};
    gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys,
                                                               gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);
    int status = 0;
#pragma omp parallel for shared(ArrayOfDots) private(sys, param, d, status)
    for (int param = 1; param < mu; param++) {

        gsl_odeiv2_system sys = {ode_func, NULL, dim, &param};
        gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys,
                                                               gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);
        for (int i = 1; i <= count; i++)
        {
            double xi = x_start + i * (x_end - x_start) / count;

            int status = gsl_odeiv2_driver_apply(d, &x, xi, y);

            if (status != GSL_SUCCESS)
            {
                printf ("error, return value=%d\n", status);
                break;
            }
           // ArrayOfDots[i].par = mu;
           // ArrayOfDots[i].x = xi;
           // ArrayOfDots[i].y = y[0];
        }
        gsl_odeiv2_driver_free (d);
    }

}

主要

int main() {
    double x_start = 0;
    double x_end = 10;
    double y_start = 0;
    int count = 10;
    struct Dots ArrayOfDots[count];
    calc_cauchy_problem(ArrayOfDots, x_start, x_end, y_start, count);
    return 0;
}

使用此gcc main.c -o main -fopenmp -lgsl -std=gnu11已成功编译,但是启动时出现错误

gsl: driver.c:354: ERROR: integration limits and/or step direction not consistent
Default GSL error handler invoked.

我认为此#pragma omp parallel for shared(ArrayOfDots) private(sys, param, d, status)的主要问题是,但我不知道如何用另一种方式重写它。 感谢您的回复。

UPD:

有了Kaveh Vahedipour的帮助,我的代码部分开始起作用。这意味着我的一半周期开始工作。 UPD UPD: 经过另一次调查,我得到了以下代码: 它可以编译并运行,但是我得到Process finished with exit code 4printf("Elapsed time = %f\n", omp_get_wtime() - start_time);什么也没打印。

struct Dots {
    double par;
    double x;
    double y;
};

int ode_func (double x, const double y[], double f[], void *params)
{

    double mu = *(int *)params;
    f[0] = (x + 2 * y[0]) / (1 + mu * mu);
    return GSL_SUCCESS;
}
void calc_cauchy_problem(double x_start, double x_end, double y_start,
                         int count, int param1, int param2) {
    int dim = 1;
    double x = x_start;
    double y[1] = {y_start};
    int param = param1;
    int j = 0;
    int status = 0;
    char filename[10];

#pragma omp parallel for private(param, status, x, y)
    for (param = param1; param <= param2; param++) {
        struct Dots ArrayOfDots[count];
        gsl_odeiv2_system sys = {ode_func, NULL, dim, &param};
        gsl_odeiv2_driver * d =
                gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);
        for (int i = 1; i <= count; i++) {
            double xi = x_start + i * (x_end - x_start) / count;

            int status = gsl_odeiv2_driver_apply(d, &x, xi, y);
            if (status != GSL_SUCCESS)
            {
                printf ("error, return value=%d\n", status);
                break;
            }

            ArrayOfDots[i].par = param;
            ArrayOfDots[i].x = xi;
            ArrayOfDots[i].y = y[0];
        }
        gsl_odeiv2_driver_free (d);

    }
}
int main() {
    double start_time = omp_get_wtime();
    double x_start = 0;
    double x_end = 10;
    double y_start = 0;
    const int count = 500;
    int param1 = 1;
    int param2 = 10;
    calc_cauchy_problem(x_start, x_end, y_start, count, param1, param2);
    printf("Elapsed time = %f\n", omp_get_wtime() - start_time);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

Sub Closing_Costs_Popup() Dim xRg As Range Dim xTxt As String Dim xCell As Range Dim xStr As String Dim xRow As Long Dim xCol As Long On Error Resume Next If ActiveWindow.RangeSelection.Count > 1 Then xTxt = ActiveWindow.RangeSelection.AddressLocal Else xTxt = ActiveSheet.UsedRange.AddressLocal End If 'Set xRg = Application.InputBox("Please select range:", "Kutools for Excel", xTxt, , , , , 8) Set xRg = Worksheets("Closing Costs").Range("AO2:AP39") If xRg Is Nothing Then Exit Sub On Error Resume Next For xRow = 1 To xRg.Rows.Count For xCol = 1 To xRg.Columns.Count xStr = xStr & vbTab & xRg.Cells(xRow, xCol).Value '& vbTab Next xStr = xStr & vbCrLf Next MsgBox xStr, vbInformation, "Settlement Charges" End Sub 添加到专用循环变量:x。如果您仍然遇到问题,请与我联系。

private(sys, param, d, status, x)

答案 1 :(得分:0)

似乎此版本可以正常工作。我认为问题出在此结构Dots ArrayOfDots[count];上,当我尝试将值推入该结构时。

      ArrayOfDots[i].par = param;
      ArrayOfDots[i].x = xi;
      ArrayOfDots[i].y = y[0];

这是完整的代码。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
// GSL lib includes
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>


int ode_func (double x, const double y[], double f[], void *params)
{

    double mu = *(int *)params;
    f[0] = (x + 2 * y[0]) / (1 + mu * mu);
    return GSL_SUCCESS;
}

void calc_cauchy_problem(double x_start, double x_end, double y_start,
                         int count, int param1, int param2) {

#pragma omp parallel for
    for(int param = param1; param < param2; param++) {
        gsl_odeiv2_system sys = {ode_func, NULL, 1, &param};

        gsl_odeiv2_driver * d =
                gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd,
                                               1e-6, 1e-6, 0.0);
        int i;
        double x = x_start, x1 = x_end;
        double y[1] = { y_start };

        for (i = 1; i <= count; i++)
        {
            double xi = i * x1 / count;
            int status = gsl_odeiv2_driver_apply (d, &x, xi, y);

            if (status != GSL_SUCCESS)
            {
                printf ("error, return value=%d\n", status);
                break;
            }

//            printf ("%d %d %.5e %.5e\n", omp_get_thread_num(), param, x, y[0]);
        }

        gsl_odeiv2_driver_free (d);
        }
    }

int main() {
    double start_time = omp_get_wtime();
    double x_start = 0;
    double x_end = 10;
    double y_start = 0;
    const int count = 100000;
    int param1 = 1;
    int param2 = 20;
    calc_cauchy_problem(x_start, x_end, y_start, count, param1, param2);
    printf("Elapsed time = %f\n", omp_get_wtime() - start_time);
    return 0;
}

非常感谢Kaveh Vahedipour。