¾Æ·¡¿Í °°ÀÌ gradient_2d¶ó´Â ÇÔ¼ö ¼±¾ð½Ã struct ÀڷḦ »ç¿ëÇÑ ´ÙÀ½¿¡ free¸¦ ½ÃÅ°°í ½ÍÀºµ¥¿ä. Á¦°¡ »ç¿ëÇÏ´Â ¹æ¹ýÀ¸·Î Çϸé mainÇÔ¼ö³ª gradient_2d ÇÔ¼ö¿¡¼ structÀڷḦ free ÇÒ ¹æ¹ýÀ» ¸ø ã°Ú³×¿ä..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cblas.h> // Use " -lcblas " flag for compilation
struct grdnt
{
double *dx, *dy, *dz;
};
struct grdnt gradient_2d(int l, int m, double in[l*m])
{
int i,
siz1 = l * l,
siz2 = m * m;
double *op1, *op2;
struct grdnt output;
output.dx = malloc(l * m * sizeof(double));
output.dy = malloc(l * m * sizeof(double));
op1 = malloc(siz1 * sizeof(double));
op2 = malloc(siz2 * sizeof(double));
memset(op1,0.0,siz1);
memset(op2,0.0,siz2);
for(i=0;i<l-1;i++)
{
op1[(l+1)*i + 1] = 1.0/2.0;
op1[(l+1)*i + l] = -1.0/2.0;
}
for(i=0;i<m-1;i++)
{
op2[(m+1)*i + 1] = 1.0/2.0;
op2[(m+1)*i + m] = -1.0/2.0;
}
cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,l,m,l,1.0,op1,l,in,m,0.0,output.dy,m);
cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasTrans ,m,l,m,1.0,op2,m,in,m,0.0,output.dx,l);
free(op1);
free(op2);
return output;
}
int main()
{
int i,j;
int p = 6, q = 8;
double arr[] = { 5.0, 8.0, 13.0, 20.0, 29.0, 40.0, 53.0, 68.0,
9.0, 12.0, 17.0, 24.0, 33.0, 44.0, 57.0, 72.0,
13.0, 16.0, 21.0, 28.0, 37.0, 48.0, 61.0, 76.0,
17.0, 20.0, 25.0, 32.0, 41.0, 52.0, 65.0, 80.0,
21.0, 24.0, 29.0, 36.0, 45.0, 56.0, 69.0, 84.0,
25.0, 28.0, 33.0, 40.0, 49.0, 60.0, 73.0, 88.0,
};
struct grdnt grid;
grid = gradient_2d(p,q,arr);
return 0;
}
Á¦°¡ ÇÏ·Á°í ÇÏ´Â °ÍÀº gradient_2dÇÔ¼ö¸¦ ÅëÇؼ 2°³ ÀÌ»óÀÇ ÀڷḦ ¾ò¾î³»·Á°í ÇÏ´Â °ÍÀä. Á¦°¡ ÇÏ°í ÀÖ´Â ¹æ¹ýÀÌ ¸Â´ÂÁöµµ ±Ã±ÝÇÕ´Ï´Ù. À§¿Í °°ÀÌ ÇÏ¸é µÇ±â´Â Çϴµ¥ ¸Þ¸ð¸®¸¦ free ÇÏÁö ¸øÇÏ´Ï arrÀÇ Å©±â°¡ ¾öû³ª°Ô Ä¿Áö¸é ¸Þ¸ð¸® ³¶ºñ°¡ ½ÉÇÒ °Í °°¾Æ¼¿ä..
°¨»çÇÕ´Ï´Ù.
Àâ´ã | 1317¸íÀÌ Àоú¾î¿ä. 18.117.196.217