MPI is used a lot in scientific/defense fields. In my experience it's also harder to learn/use than the other parallel programming models, so maybe it will look good on the CV?
What broader ideas do you feel like you're missing? I think most companies are happy if you understand mutexes, semaphores, fences, and basic concepts of non-determinism/race conditions.
Things like threading, thread synchronization, mutexes, and semaphores, which were introduced in the OS class but didn't get very detailed.
What I really expected and wanted to learn were the kinds of things you are describing. Things like threading, thread synchronization, mutexes, and semaphores, which were introduced in the OS class but didn't get very detailed. The course description says the class would teach techniques for designing parallel algorithms, like balanced trees, pointer jumping, partitioning, pipelining, etc. but it doesn't look like we'll get into any of that.
I have a question for c++ programmers out there. Have you ever heard of/used Message Passing Interface (MPI) in your work?
I'm taking a parallel programming course that seems entirely devoted to this instead of the broader ideas of writing parallel algorithms. I'm now really concerned if learning all this is going to be at all useful.
I have a question for c++ programmers out there. Have you ever heard of/used Message Passing Interface (MPI) in your work?
I'm taking a parallel programming course that seems entirely devoted to this instead of the broader ideas of writing parallel algorithms. I'm now really concerned if learning all this is going to be at all useful.
A couple of questions to cement my understanding of struct vs class:
1) What is a non-obvious benefit to using either one of these in a program? I understand that it helps with organizing your program by not having a set of different variables laying around but that is about it.
2) Are there any other differences between struct and class other than having to explicitly state whether something is private or public respectively? Why would one choose a class over a struct? Or is it just a preference thing?
An entertaining thought exercise I've seen trip up more than a few people both in classrooms and in the professional world:
You have an std vector, myvector, which was declared as follows:
std::vector myvector<Object *>;
There is one entry in the vector (in other words, size = 1).
You want to properly delete the entry from the vector.
What, if anything, is wrong with the following method of doing so? And how can it be fixed if it is an issue?
delete &myvector[0];
Off the top of my head, it frees the memory belonging to the object, but the pointer stays in the vector. You'd have to call myvector.erase(0); too (still calling delete so the memory gets freed).
for (auto it=v.begin(); it!=v.end(); ++it)
delete *it;
v.clear();
delete vector[0];
Is correct. The OP's version is calling delete on an Object**
The erase issue is still true though, but just use vector.clear()
A common cleanup pattern is
Code:for (auto it=v.begin(); it!=v.end(); ++it) delete *it; v.clear();
Oops, missed that one. Wouldn't the compiler detect that, though?
delete &myvector[0] has the effect of freeing up the memory used to store the Object *.
At that point, there is a risk of a memory leak: The object still resides in memory allocated for it. but the area of memory which tells us where the object is located can be written to by anything.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Fraction {
private:
int num,denom;
public:
//Constructors
[B] Fraction (char *s); //string constructor[/B]
Fraction (Fraction const &src) {set(src.num,src.denom);} //copy constructor
Fraction () {num = 0; denom =1; } //default constructor
Fraction (int n, int d) {set (n,d);}
//Member functions
void set(int n, int d) {num = n, denom = d, normalize();}
int get_num() {return num;}
int get_denom() {return denom;}
Fraction add (Fraction other);
Fraction multi (Fraction other);
private:
int gcf (int a,int b);
void normalize ();
int lcm (int a, int b) {int n = gcf (a, b) ; return a / n * b;}
};
[B]
int main (){
Fraction arr_frac[3] = {"1/2", "3/4", "4/6"};
for (int i=0; i < 4; i++)
cout << arr_frac[i].get_num() << "/" << arr_frac[i].get_denom() << endl;
return 0;
}[/B]
void Fraction::normalize(){
if (denom == 0 || num == 0){
num = 0;
denom = 1;
}
if (denom < 0) {
num = num * -1;
denom = denom * -1;
}
int n = gcf(num, denom);
num = num / n;
denom = denom / n;
}
int Fraction::gcf (int a, int b){
if (b == 0)
return abs(a);
else
return gcf (b, a%b);
}
Fraction Fraction::add (Fraction other) {
Fraction fract;
int lcd = lcm (denom, other.denom);
int quot1 = (lcd / denom);
int quot2 = (lcd / other.denom);
fract.set(num * quot1 + other.num * quot2, lcd);
return fract;
}
Fraction Fraction::multi(Fraction other){
Fraction fract;
fract.set(num * other.num, denom * other.denom);
return fract;
}
[B]Fraction::Fraction(char *s) {
int n = 0;
int d = 1;
cout << "Using string constructor ";
char *p1 = strtok(s, "/, ");
char *p2 = strtok(NULL, "/, ");
if (p1 !=NULL)
n = atoi(p1);
if (p2 !=NULL)
d = atoi(p2);
set(n,d);
}[/B]
Okay, I need help I'm chugging along trying to teach myself C++, and I'm getting into Classes and constructors. So this book has me working with a Fraction class and a string constructor so you can create arrays of fraction objects by using strings "1/2" "3/4" etc.
I keep getting "warning: depreciated conversion from string constant to 'char*'" the program runs but crashes immediately prints out "Using string constructor" but that's it. If I just use one number : "5" it works for the default case and makes it "5/1" but it fails as soon as I try to enter the whole fraction.
I haven't programmed the CellBE, but imagine programming the SPU's is very similar (you explicitly have to DMA the memory you want to an SPE). The benefit for us is that caches, shared memories, and their management hardware can be power hungry and thus can be omitted from the chip (at the expense of the programmer).
Thanks to both of you. Yeah this book is really odd, it feeds me wrong programs that need fixing all the time. It's called "C++ Without Fear Second Edition" it's meant to be for complete beginners to programming.
You can see, it gives you the function for that string constructor with none of the warnings about string literals that you guys gave me, I'm thinking I might need to ditch and get a new book :\.
-snip-
int n = 0;
int d = 1;
char *dstr = NULL;
if(s != NULL)
{
dstr = strstr(s, "/");
n = atoi(s);
}
if(dstr != NULL)
{
d = atoi(dstr + 1);
}
if(d == 0)
{
//handle exception
}
Thanks to both of you. Yeah this book is really odd, it feeds me wrong programs that need fixing all the time. It's called "C++ Without Fear Second Edition" it's meant to be for complete beginners to programming.
You can see, it gives you the function for that string constructor with none of the warnings about string literals that you guys gave me, I'm thinking I might need to ditch and get a new book :\.
I agree. Here's some recommendations: tinyurl.com/so-cxxbooks
Though, are you a complete beginner to programming? If you already have experience in other languages, that might affect the recommendations.
A note: You don't really need to break the fraction string into tokens.
Something like this would be better and cheaper if you must rely on c constructs for implementation.
Code:int n = 0; int d = 1; char *dstr = NULL; if(s != NULL) { dstr = strstr(s, "/"); n = atoi(s); } if(dstr != NULL) { d = atoi(dstr + 1); } if(d == 0) { //handle exception }
atoi always converts a number up til a non-digit character is encountered. Thus, to atoi, the '/' naturally splits the character array anyway.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
cout << "Please input two numbers" << endl;
cin >> s1;
cin >> s2;
cout << endl;
if(s1 > s2)
cout << s1 << " is the larger number" << endl;
else if(s2 > s1)
cout << s2 << " is the larger number" << endl;
return 0;
}
Giving us an example of output would help here.With what I have so far I can input two variable but for some reason it seems to only be using the first digit of the two numbers inputted. Why is that?
So I am taking a basic C++ class as my first programming foray so forgive me if this is a little too simple
I need to write a program that asks the user to input two integers. Store the integers in two separate variables and using the conditional operator determine which integer is the smaller of the two. With what I have so far I can input two variable but for some reason it seems to only be using the first digit of the two numbers inputted. Why is that?
Code:#include <iostream> #include <string> using namespace std; int main() { string s1, s2; cout << "Please input two numbers" << endl; cin >> s1; cin >> s2; cout << endl; if(s1 > s2) cout << s1 << " is the larger number" << endl; else if(s2 > s1) cout << s2 << " is the larger number" << endl; return 0; }
string s1,s2;
int s1, s2;
Alas, I moved into C++ having come through other languages. I don't really have any personal suggestions for someone learning C++ as their first language.I've been using this book for a few months, but aside from that yes. The 2-3 months I've spent with C++ so far is the extent of my programming knowledge. I'm thinking about ordering C++ Primer Plus 6th edition, would you recommend it?
ChangeintoCode:string s1,s2;
Code:int s1, s2;
string is a text string where as integers are dealing with the actual numbers.
Hmm, thank you. I was trying to get it to accept two inputs earlier but I clearly must've been doing something very wrong
Thanks to both of you. Yeah this book is really odd, it feeds me wrong programs that need fixing all the time. It's called "C++ Without Fear Second Edition" it's meant to be for complete beginners to programming.
You can see, it gives you the function for that string constructor with none of the warnings about string literals that you guys gave me, I'm thinking I might need to ditch and get a new book :\.
Don't make PI an argument. It's supposed to be a constant, so you should define and declare it outside of any method. Like, in line 2, just write
#include<stdio.h>
//Declarations
void input(double *);
double Ang_Vel (double, double, double);
double tip_speed (double, double, double);
void output(double, int, double);
int main ()
{
//Variables
double d = 0.0; //diameter of propeller in meters
int s = 0.0; //shaft speed of propeller
double w = 0.0; //angular velocity
double t = 0.0; //tip speed
double PI = 0.0; //PI
//Get input
input(&d);
//Calculate acceleration for various RPMs
for (s = 1000; s < 10400; s = s + 1000)
{
w = Ang_Vel(s,PI,w);
t = tip_speed(w,d,t);
output(d,s,t);
}
return 0;
}
//Input Function
void input(double *d)
{
int done = 0;
while (done == 0)
{
printf("Enter propeller diameter in meters:\n");
scanf("%lf",d);
if (*d > 0.0)
{
done = 1;
}
else
{
printf("Invalid propeller diameter\n");
}
}
}
//Calculation Functions
//Angular velocity
double Ang_Vel(double s, double PI, double w)
{
PI = 3.14159265;
w = (2 * PI * s); //angular velocity
return w;
}
// Tip Speed
double tip_speed(double w, double d, double t)
{
t = (w * d)/ 60;
return t;
}
//Output Function
void output (double d, int s, double t)
{
printf("A propeller with a Diameter of %f meters and a Shaft Speed of %d rpm, has a tip speed of %f meters/second\n", d, s, t);
}
Enter propeller diameter in meters:
2.10
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 1000 rpm, has a tip speed of 219.911485 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 2000 rpm, has a tip speed of 439.822971 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 3000 rpm, has a tip speed of 659.734457 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 4000 rpm, has a tip speed of 879.645942 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 5000 rpm, has a tip speed of 1099.557428 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 6000 rpm, has a tip speed of 1319.468913 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 7000 rpm, has a tip speed of 1539.380399 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 8000 rpm, has a tip speed of 1759.291884 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 9000 rpm, has a tip speed of 1979.203370 meters/second
A propeller with a Diameter of 2.100000 meters and a Shaft Speed of 10000 rpm, has a tip speed of 2199.114855 meters/second
How to I limit the output to 2 places after the decimal?
#include<stdio.h>
int main ()
{
int i = 0;
printf("How much of the Fibonacci Sequence would you like to display? (Enter an Integer 0 or greater):\n");
scanf("%d", &i);
fibonacci(&i);
return 0;
}
// Fibonacci Sequence Function
int fibonacci(int i)
{
int a = 0;
int b = 1;
int sum;
int n;
for (n=0; n <= i; n++)
{
printf("%d\n",a);
sum = a + b;
a = b;
b = sum;
}
return 0;
}
My problem is that the loop doesn't stop once it reaches the value of 'i'. It just continues and I have to close my compiler. I've got the for loop formatted right, and I don't understand why the loop doesn't end.
I'm not sure what you're talking about. You either use pointers or you don't. There's no "you have to". You can always just operate on data.I had thought that since I didn't declare the functions with pointers (*) prior to the main function (like I did in the code in Post #1235) that I would have to use pointers in the main function?? Something to that effect was mentioned in lecture, but I must have been confused about the context.
#include<stdio.h>
int main ()
{
int i = 0;
[B]int *pointer;[/B]
printf("How much of the Fibonacci Sequence would you like to display? (Enter an Integer 0 or greater):\n");
scanf("%d", &i);
[B]pointer = &i;[/B]
fibonacci([B]pointer[/B]);
return 0;
}
// Fibonacci Sequence Function
int fibonacci([B]int *pointer[/B])
{
int a = 0;
int b = 1;
int sum;
int n;
for (n=0; n <= [B]*pointer[/B]; n++)
{
printf("%d\n",a);
sum = a + b;
a = b;
b = sum;
}
return 0;
}
I used to know C++ in university, but haven't used it in a while. I want to do some recreational programming and refresh myself with the language. Is there a free compiler and assembler I can download to get started now? What's the best?
I used to know C++ in university, but haven't used it in a while. I want to do some recreational programming and refresh myself with the language. Is there a free compiler and assembler I can download to get started now? What's the best?
I used to know C++ in university, but haven't used it in a while. I want to do some recreational programming and refresh myself with the language. Is there a free compiler and assembler I can download to get started now? What's the best?
I used to know C++ in university, but haven't used it in a while. I want to do some recreational programming and refresh myself with the language. Is there a free compiler and assembler I can download to get started now? What's the best?