我在C中构造函数的偏导数.该过程主要由大量小循环组成.每个循环负责填充矩阵的列.因为矩阵的大小很大,所以应该有效地编写代码.我对实施有很多计划,我不想深入了解细节.
我知道智能编译器会尝试自动利用缓存.但我想了解更多使用缓存和编写高效代码和高效循环的细节.如果提供一些资源或网站,我将不胜感激,因此我可以更多地了解如何在减少内存访问时间和利用优势方面编写有效代码.
我知道我的请求看起来很草率,但我不是电脑人.我做了一些研究但没有成功.所以,任何帮助表示赞赏.
谢谢
我对此很新,所以请耐心等待.对于编码C类,我们总共使用九个函数和一个头文件(我们称之为my.h)来收集房间的长度和宽度(int),折扣百分比(也是一个int) ),和地毯的单价(双倍).我们将使用输入来计算安装地毯的总成本.我可以得到打印的长度,宽度和面积,但不是单价.它从Read_Data.c和Calc_Value.c函数打印frin,但不在Install_Calice.c中调用,而是在Calc_Value.c中调用.unit_price与长度和宽度同时读取,但不知何故,它没有正确传递.我不知道为什么.也许它需要一个不同的指针.任何帮助都会非常感激.我已经阅读了我的书,并与我的教授和同学交谈,并在互联网上搜索,但我找不到任何有用的东西.Install_Price的代码是:
/*This function calculates the cost of the carpet and the labor cost in order to
calculate the installed price.*/
#include "my.h"
void Install_Price (int length, int width, int unit_price, int* area,
double* carpet_cost, double* labor_cost, double* installed_price)
{
printf("\nThe unit price is %7.2f.\n", *unit_price);
*area = length * width;
*carpet_cost = (*area) * unit_price;
printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);
*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);
return;
} …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的程序,我正在努力提高性能.我知道的一种方法是帮助使用SSE3(因为我工作的机器支持这个),但我完全不知道如何做到这一点.这是一个代码片段(c ++):
int sum1, sum2, sum3, sum4;
for (int i=0; i<length; i+=4) {
for (int j=0; j<length; j+=4) {
sum1 = sum1 + input->value[i][j];
sum2 = sum2 + input->value[i+1][j+1];
sum3 = sum3 + input->value[i+2][j+3];
sum4 = sum4 + input->value[i+3][j+4];
{
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于此的内容,并理解了这个想法,但我完全不知道如何实现这一点.有人可以帮帮我吗?我认为这很简单,特别是对于我的简单程序,但有时候入门是最难的部分.
谢谢!
我收到了错误"lvalue required as left operand of assignment".
它分配给该行:
if(ch=='.' || ch=='"' || ch=='(' || ch=')' || ch=='!' || ch==',' || ch=='?' || ch==';') { etc }
Run Code Online (Sandbox Code Playgroud)
我只想检查我正在看的角色是否等于这些角色中的任何一个.关于修复的任何想法?
谢谢,
看看这段代码:
#include <stdio.h>
#include <omp.h>
int main()
{
long i, j;
#pragma omp for
for(i=0;i<=100000;i++)
{
for(j=0;j<=100000;j++)
{
if((i ^ j) == 5687)
{
//printf("%ld ^ %ld\n", i, j);
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,结果:
robotex@robotex-work:~/Projects$ gcc test.c -fopenmp -o test_openmp
robotex@robotex-work:~/Projects$ gcc test.c -o test_noopenmp
robotex@robotex-work:~/Projects$ time ./test_openmp
real 0m11.785s
user 0m11.613s
sys 0m0.008s
robotex@robotex-work:~/Projects$ time ./test_noopenmp
real 0m13.364s
user 0m13.253s
sys 0m0.008s
robotex@robotex-work:~/Projects$ time ./test_noopenmp
real 0m11.955s
user 0m11.853s
sys 0m0.004s
robotex@robotex-work:~/Projects$ time ./test_openmp
real 0m15.048s
user 0m14.949s …Run Code Online (Sandbox Code Playgroud) 我听说过它提到了几次,std::vector在存储原始指针时并不是异常安全的,而且应该使用unique_ptr或shared_ptr替代.
我的问题是,为什么std::vector不是异常安全的,这些类如何解决这个问题?
我知道为了malloc指向数组的指针数组,语法应该是:
(int**)malloc(numberOfDesiredElements*sizeof(int*))
Run Code Online (Sandbox Code Playgroud)
不小心忘了把numberOfDesiredElements*size放在前面。它搞乱了我的程序抛出随机分段错误,即有时程序输出正确,有时它出现段错误。
有人可以解释当我没有指出我想要多少个插槽时发生了什么吗?
谢谢!
我使用OpenMP,我遇到了错误结果的问题.
这是代码:
#pragma omp parallel shared(L,nthreads,chunk) private(tid,i,j){
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Starting matrix multiple example with %d threads\n",nthreads);
printf("Initializing matrices...\n");
}
#pragma omp for schedule (static, chunk)
for( i=0; i<SIZE_A;i++){
for( j=0; j<SIZE_B;j++){
if(A[i]==B[j]){
if(i==0 || j==0)
L[i][j]=1;
else
L[i][j] = L[i-1][j-1] + 1;
}
// or reset the matching score to 0
else
L[i][j]=0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你怎么想,为什么我得到了结果?我应该改变什么?
非常感谢!
这个程序基本上将中缀转换为后缀.问题在于,-并/成为笑脸像这样:)
问题是,它显示不同的字符而不是预期的减号或分号.
// the program is used to convert a infix expression to a postfix expression
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
using namespace std;
const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;
int precedence(char ch); // function to get the precedence of the operator
char pop(); //function to pop an element from the stack
char topelement(); // returns the top element of the stack
void push(char ch); // pushes an element into the stack
int …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用内联汇编的示例:http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html 但是有些事让我感到困惑:
"好吧,当GCC能够确切地知道你之前和之后对寄存器做了什么时,这确实很有帮助......如果你告诉它将(x + 1)放入寄存器中,它甚至足够聪明. ,如果你没有破坏它,后来C代码引用(x + 1),并且它能够保持该寄存器空闲,它将重用计算.哇."
关于clobber列表的教程中存在一些不一致:
对于输入/输出列表中指定的寄存器,不需要将它们放入clobber列表中,因为GCC知道; 但是在关于rep_movsl(或rep_stosl)的示例中:
asm("cld \n\t""rep \n\t""stosl":/*无输出寄存器*/:"c"(计数),"a"(fill_value),"D"(dest):" %ecx","%edi");
虽然"S,D,c"在输出操作数中,但它们再次被列为破坏.我在C中尝试了一个简单的片段:
#include<stdio.h>
int main()
{
int a[] = {2, 4, 6};
int b[3];
int n = 3;
int v = 12;
asm ("cld\n\t"
"rep\n\t"
"movsl"
:
: "S" (a), "D" (b), "c" (n)
: );
// : "%ecx", "%esi", "%edi" );
printf("%d\n", b[1]);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用评论的clobber列表,GCC会抱怨:
ac:8:3:错误:在重新加载'asm'ac时,无法在类'CREG'中找到寄存器:8:3:错误:'asm'操作数有不可能的约束
如果我使用空的clobber列表,它将编译并输出为4.