查找每个矩阵行中最大数的列索引的高效C代码
我已经编写了一个C代码来查找nxn矩阵中每行的最大数(绝对最大数)的列索引。然而,有一个条件!如果当前行中max number的列索引与前一行中的一行相同,则程序应跳过该索引并查找该行中的下一个max 我的代码运行良好,但性能是主要问题。不幸的是,到目前为止,由于依赖关系,我没有成功地使用OpenMP并行化代码。如果您能帮助提高我的代码的性能,我将不胜感激。先谢谢你 代码如下:查找每个矩阵行中最大数的列索引的高效C代码,c,C,我已经编写了一个C代码来查找nxn矩阵中每行的最大数(绝对最大数)的列索引。然而,有一个条件!如果当前行中max number的列索引与前一行中的一行相同,则程序应跳过该索引并查找该行中的下一个max 我的代码运行良好,但性能是主要问题。不幸的是,到目前为止,由于依赖关系,我没有成功地使用OpenMP并行化代码。如果您能帮助提高我的代码的性能,我将不胜感激。先谢谢你 代码如下: #include <stdio.h> #include <stdlib.h> #include
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <limits.h>
int main ( unsigned long int argc, char *argv[] )
{
int n = 3;
//double A[9] = {1,2,3,4,5,6,7,8,9}; //output: ind_col[1,..,3] = 2,1,0 max_val[1,..,3] = 3,5,7
double A[9] = {1,3,2,4,6,5,7,8,9}; //output: ind_col[1,..,3] = 1,2,0 max_val[1,..,3] = 3,5,7
/* ind_col is 1xn array that contains the column index of abs. max number for each row */
int *ind_col = NULL;
ind_col = (int*) calloc(n,sizeof(int));
/* max_val is 1xn array that contains the abs. max number for each row */
double *max_val = NULL;
max_val = (double*) calloc(n,sizeof(double));
int i,j,k,rep = 0;
for(i=0; i<n; i++){
for(j=0; j<n; j++) {
if ( (fabs(A[i*n+j]) < max_val[i]) ) continue; // if a new max is found, do...
for(k=0; k<i; k++) if (ind_col[k] == j) rep = 1; // check if the same column index was not previously used
if (rep != 1) { // if this is a new column index save it
max_val[i] = fabs(A[i*n+j]);
ind_col[i] = j;
}
rep = 0;
}
}
for(i=0; i<n; i++) printf("ind_col[%i] = %i , val = %f\n", i, ind_col[i], A[i*n+ind_col[i]]);}
}
#包括
#包括
#包括
#包括
#包括
int main(无符号长整型argc,char*argv[])
{
int n=3;
//双A[9]={1,2,3,4,5,6,7,8,9};//输出:ind_col[1,…,3]=2,1,0 max_val[1,…,3]=3,5,7
双A[9]={1,3,2,4,6,5,7,8,9};//输出:ind_col[1,…,3]=1,2,0 max_val[1,…,3]=3,5,7
/*ind_col是1xn数组,包含每行abs.max number的列索引*/
int*ind_col=NULL;
ind_col=(int*)calloc(n,sizeof(int));
/*max_val是包含每行abs.max编号的1xn数组*/
double*max_val=NULL;
max_val=(double*)calloc(n,sizeof(double));
int i,j,k,rep=0;
对于(i=0;i使用位掩码标记所用列的编号:
[您也可以使用字符或整数的普通指示符数组]
#定义ZBIT(字符位*大小zzz[0])
#定义ZTEST(z)(zzz[z/ZBITS]&(1u这是最终代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <limits.h>
unsigned *Find_Pivot(double *A,unsigned n)
{
unsigned *ind_col ;
unsigned *mark;
unsigned irow,jcol;
ind_col = calloc(n,sizeof *ind_col);
mark = calloc(n, sizeof *mark);
for(jcol=0; jcol<n; jcol++) { ind_col[jcol] = n; } // sentinel
for(jcol=0; jcol<n; jcol++) { mark[jcol] = n; } // sentinel
double max = 0;
double zabs = 0;
for(irow=0; irow<n; irow++){
max = 0;
for(jcol=0; jcol<n; jcol++) {
zabs = fabs(A[irow*n+jcol]) ;
if (zabs < max) continue; // no new max is found
if (mark[jcol] < irow) continue;// check if the same column index was used by a previous row
if (jcol > 0) { //undo previous submax
unsigned ocol = ind_col[irow] ;
if (ocol <jcol) mark[ocol] = n; // reset sentinel ...
}
mark[jcol] = irow; // mark our row index in here ...
max = zabs;
ind_col[irow] = jcol;
}
}
free(mark);
return ind_col;
}
int main ( unsigned long int argc, char *argv[] )
{
unsigned *ind_col;
int n = 3;
double A[9] = {6,3,2,9,8,8,10,8,9};
ind_col = Find_Pivot(A, n);
int i;
for (i=0;i < n;i++) printf("%i:=%i\n", i, ind_col[i]);
return 0;
}
#包括
#包括
#包括
#包括
#包括
无符号*Find_轴(双*A,无符号n)
{
无符号*ind_col;
无符号*标记;
未签名的irow,jcol;
ind_col=calloc(n,sizeof*ind_col);
mark=calloc(n,sizeof*mark);
对于(jcol=0;jcolifn
小于64(或32),可以使用位掩码进行排除。如果较大,可以使用位掩码数组。我尝试了您的代码,但它不起作用。ind_col[0,…,n]=(0,1,…,n)一直都是。我无法测试它,因为你问题中的代码不完整,不可编译和测试。因此,我的答案中可能有错误。等等:我必须取消设置位(从上一个最大值)如果第二次发现最大值,它将被忽略。brb非常感谢。我还编辑了您的代码,以便对其进行测试。在这种情况下,正确的输出应该是2,1,0。我将使用(改进的?)版本更新答案。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned *ihaveaname(double *A,unsigned n)
{
unsigned *ind_col ;
double *max_val ;
unsigned *mark;
unsigned irow,jcol;
ind_col = calloc(n,sizeof *ind_col);
max_val = calloc(n,sizeof *max_val);
mark = calloc(n, sizeof *mark);
for(jcol=0; jcol<n; jcol++) { ind_col[jcol] = n; } // sentinel
for(jcol=0; jcol<n; jcol++) { mark[jcol] = n; } // sentinel
for(jcol=0; jcol<n; jcol++) { max_val[jcol] = 0.0; }
for(irow=0; irow<n; irow++){
for(jcol=0; jcol<n; jcol++) {
double zabs;
zabs = fabs(A[irow*n+jcol]) ;
if (zabs < max_val[irow]) continue; // no new max is found
if (mark[jcol] < irow) { // check if the same column index was used by a previous row
// fprintf(stderr,"[Skip col%u row%u]", jcol,irow);
continue;
}
if (jcol > 0) { //undo previous submax
unsigned ocol;
ocol = ind_col[irow] ;
if (ocol <jcol) {
mark[ocol] = n; // reset sentinel ...
// fprintf(stderr,"[Undo ocol%u]", ocol);
}
}
// fprintf(stderr,"[Mark col%u <- row%u]", jcol,irow);
mark[jcol] = irow; // mark our row index in here ...
max_val[irow] = zabs;
ind_col[irow] = jcol;
}
// fprintf(stderr,"Max[%u] = %f\n", irow,max_val[irow]);
}
free(mark);
free(max_val);
return ind_col;
}
int main(void)
{
unsigned uu, *uuu;
double array[9]={
1,2,3,
2,3,1,
3,1,2};
uuu = ihaveaname(array, 3);
for (uu=0;uu < 3;uu++){
printf("%u:=%u\n", uu, uuu[uu]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <limits.h>
unsigned *Find_Pivot(double *A,unsigned n)
{
unsigned *ind_col ;
unsigned *mark;
unsigned irow,jcol;
ind_col = calloc(n,sizeof *ind_col);
mark = calloc(n, sizeof *mark);
for(jcol=0; jcol<n; jcol++) { ind_col[jcol] = n; } // sentinel
for(jcol=0; jcol<n; jcol++) { mark[jcol] = n; } // sentinel
double max = 0;
double zabs = 0;
for(irow=0; irow<n; irow++){
max = 0;
for(jcol=0; jcol<n; jcol++) {
zabs = fabs(A[irow*n+jcol]) ;
if (zabs < max) continue; // no new max is found
if (mark[jcol] < irow) continue;// check if the same column index was used by a previous row
if (jcol > 0) { //undo previous submax
unsigned ocol = ind_col[irow] ;
if (ocol <jcol) mark[ocol] = n; // reset sentinel ...
}
mark[jcol] = irow; // mark our row index in here ...
max = zabs;
ind_col[irow] = jcol;
}
}
free(mark);
return ind_col;
}
int main ( unsigned long int argc, char *argv[] )
{
unsigned *ind_col;
int n = 3;
double A[9] = {6,3,2,9,8,8,10,8,9};
ind_col = Find_Pivot(A, n);
int i;
for (i=0;i < n;i++) printf("%i:=%i\n", i, ind_col[i]);
return 0;
}