---
title: KmlScaissGmresSetUserPreconditioner?I
description: "关联用户自定义预条件子回调函数，如不使用自定义的预条件子，该接口无需使用。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0938.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0938.html
indexId: 66e23db2ccfaca77bf607734823958e0d77bbf45d1131767a089a8ae5fd53a6a61
---
# KmlScaissGmresSetUserPreconditioner?I

关联用户自定义预条件子回调函数，如不使用自定义的预条件子，该接口无需使用。

#### 接口定义

C Interface：

int KmlScaissGmresSetUserPreconditionerSI(KmlScasolverTask **handle, void *ustruct, int (*fptr)(void *ustruct, float *x));

int KmlScaissGmresSetUserPreconditionerDI(KmlScasolverTask **handle, void *ustruct, int (*fptr)(void *ustruct, double *x));


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| handle | KmlScasolverTask \*\* | 求解器句柄，传入之前步骤的变量。 | 输入/输出 |
| ustruct | void \* | 指向用户数据的指针，每次调用预条件子时作为第一个参数传递。 | 输入 |
| (\*fptr) | int | 用户自定义的函数指针，指向回调函数，该回调函数进行预条件子与其第二参数间的计算。 | 输入 |


#### 返回值

| 返回值 | 类型 | 描述 |
| --- | --- | --- |
| KMLSS\_NO\_ERROR | int | 正常执行。 |
| KMLSS\_NULL\_ARGUMENT | int | handle，fptr中存在空参数。 |


#### 依赖

#include "kml_scaiss.h"


#### 示例

C Interface：```
// USER data struct
typedef struct {
    int n;
    const int *ia;
    const int *ja;
    const double *a;
} duser;

int diagonal_preconditioner(void *usr, double *x)
{
    duser *u = (duser *)usr;
    int i, j;

    /* Apply diagonal preconditioner*/
    for (i = 0; i < u->n; i++) {
        for (j = u->ia[i]; j < u->ia[i + 1]; j++) {
            if (u->ja[j] == i)
                x[i] /= u->a[j];
        }
    }
    return 0;
}

MPI_Init(NULL, NULL);
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);//获取总进程数
MPI_Comm_rank(MPI_COMM_WORLD, &rank);//获取当前进程标识

int mat_size = 8;
int n = mat_size / size;
int n_beg = n * rank;
if (n * size != mat_size && rank == (size - 1)) {
    n = mat_size - n * rank;
}
int ja[26] = { 0, 3, 4, 1, 2, 3, 5, 1, 2, 7, 0, 1, 3, 6, 0, 4, 5, 1, 4, 5, 7, 3, 6, 2, 5, 7 };
double a[26] = { 1.0, 1.0, 2.0, 9.0, 2.0, 1.0, -3.0, 2.0, 3.0, 2.0, 1.0, 1.0, 9.0, -5.0, 2.0, 6.0, 1.0, -3.0, 1.0, 4.0, 1.0, -5.0, 7.0, 2.0, 1.0, 2.0 };
int ia[9] = { 0, 3, 7, 10, 14, 17, 21, 23, 26 };
int a_beg = ia[n_beg];
for (int i = n_beg; i < (n_beg + n + 1); i++) {
    ia[i] -= a_beg;
}

/* Internal KML_SCAISS structure */
KmlScasolverTask *handle;

/* KML_SCAISS control parameters */
int error;            /* Output error handle */

/* Create data structures */
const double *a_holder = &a[a_beg];
const int *ja_holder = &ja[a_beg];
const int *ia_holder = &ia[n_beg];
error = KmlScaissGmresInitStripesDI(&handle, mat_size, 1, &n, &n_beg, &a_holder, &ja_holder, &ia_holder, MPI_COMM_WORLD);
    
duser prea;
prea.n = n;
prea.ia = &ia[n_beg];
prea.ja = &ja[a_beg];
prea.a = &a[a_beg];

error = KmlScaissGmresSetUserPreconditionerDI(&handle, &prea, &diagonal_preconditioner);
if (error != 0) {
    printf("\nERROR in KmlScaissGmresSetUserPreconditionerDI: %d", error);
    return 1;
}
```
