I am using MPI for solving PDE. For this, I breakdown the 2D domain into different cells (size of each of these cells is "xcell,ycell" with xcell = size_x_domain/(number of X subdomains) and ycell = size_y_domain/(number of Y subdomains).
So, I am running the code with number of processes = (number of X subdomains)*(number of Y subdomains)
The gain relatively to sequential version is that I communicate between each process representing the sub-domains.
Here a figure illustrating my breakdown with 8 processes (2 subdomains for X and 4 for Y) :
(xs,xe) represent x_start and x_end of the cell,
(ys,ye) represent y_start and y_end of the cell
I would like to know if I have to set, into x(i,j) array, i the index as row index and j as column index ?
Is it a general rule to put the first index for row and the second one for column ? ( for example, in C, Fortran and Matlab language or maybe more)
Thanks for your help.
I'm not sure, but maybe try having a different flag for all 4 of the communications.
Related
Reading the book of Aziz & Prakash 2021 I am a bit stuck on problem 3.7 and the associated solution for which I am trying to implement.
The problem says :
You have n users with unique hashes h1 through hn and
m servers, numbered 1 to m. User i has Bi bytes to store. You need to
find numbers K1 through Km such that all users with hashes between
Kj and Kj+1 get assigned to server j. Design an algorithm to find the
numbers K 1 through Km that minimizes the load on the most heavily
loaded server.
The solution says:
Let L(a,b) be the maximum load on a server when
users with hash h1 through ha are assigned to servers S1 through Sb in
an optimal way so that the max load is minimised. We observe the
following recurrence:
In other words, we find the right value of x such that if we pack the
first x users in b - 1 servers and the remaining in the last servers the max
load on a given server is minimized.
Using this relationship, we can tabulate the values of L till we get
L(n,m). While computing L(a,b) when the values of L is tabulated
for all lower values of a and b we need to find the right value of x to
minimize the load. As we increase x, L(x,b-1) in the above expression increases the the sum term decreases. We can do binary search for x to find x that minimises their max.
I know that we can probably use some sort of dynamic programming, but how could we possibly implement this idea into a code?
The dynamic programming algorithm is defined fairly well given that formula: Implementing a top-down DP algorithm just needs you to loop from x = 1 to a and record which one minimizes that max(L(x,b-1), sum(B_i)) expression.
There is, however, a simpler (and faster) greedy/binary search algorithm for this problem that you should consider, which goes like this:
Compute prefix sums for B
Find the minimum value of L such that we can partition B into m contiguous subarrays whose maximum sum is equal to L.
We know 1 <= L <= sum(B). So, perform a binary search to find L, with a helper function canSplit(v) that tests whether we can split B into such subarrays of sum <= v.
canSplit(v) works greedily: Remove as many elements from the start of B as possible so that our sum does not exceed v. Repeat this a total of m times; return True if we've used all of B.
You can use the prefix sums to run canSplit in O(m log n) time, with an additional inner binary search.
Given L, use the same strategy as the canSplit function to determine the m-1 partition points; find the m partition boundaries from there.
I have something of a basic question as I do some preliminary coding in preparation for my dissertation. I have some experience with R, but am still somewhat new. I've looked all over the internet, and haven't found a good answer yet. Hope someone out there can help improve my code and make it more efficient.
I'm trying to create a series of 4 randomly-drawn 5x5 networks that change slightly at each time point. To do that, I create a vector of 25 randomly drawn (prob=.5) 0s and 1s, and then create a 5x5 matrix from the vector. The matrix will serve as the adjacency matrix for each network. Creating the initial matrix is pretty easy:
a <- rbinom(25, 1, .5)
matrix_a <- matrix(a, ncol = 5, nrow = 5)
This matrix will serve as my network at time point 1. For time points 2-4, I want 5 randomly-selected cells to flip, so a 0 becomes a 1, and a 1 becomes a 0. For those unfamiliar with networks, that means in five instances edges change and are added (if there wasn't one before) or are removed (if there was).
The way I've figured out how to do that is to first select 5 elements from the vector b at random:
spot <- sample(25,5)
This will give me a vector of 5 elements representing a randomly-drawn position from 1 to 25. Next, I want to change those 5 zeroes or ones to their opposite (so a zero becomes a one and vice versa), and then I can insert them back into the 25-vector element, and make matrix_b at time point 2 from that, and repeat two more times. This way, the networks stay fairly stable, but change slightly and at random at time points 2 through 4.
But here's where I'm having trouble. I'd like to create a function to automate changing the five zeroes to ones and vice versa, which seems like it should be easy to do. So far, this is the best I've been able to pull off:
x <- (a[spot])
y1 <- if (x[1]==0) {
x[1]+1
} else {
x[1]-1
}
y1
y2 <- if (x[2]==0) {
x[2]+1
} else {
x[2]-1
}
y2
I've tested this, and it does change a zero to a one and vice versa.
I repeat that three more times to create y3, y4, and y5, then create a new vector of 5 elements:
y <- c(y1,y2,y3,y4,y5)
y
Now I replace five elements from the 25-element vector a with the vector y above (which have changed from zeroes to ones and vice versa) to create the new vector b:
b <- a
b[spot] <- y
matrix_b <- matrix(b, ncol = 5, nrow = 5)
I wind up with matrix_b at time point 2, in which with 5 cells have changed from zero to one or vice versa representing edges that have been added or dropped.
This will work, but it's really inefficient. I know there's a way to automate--using functions? apply?--creating y1 through y5 above. But I've been looking for hours, and this is still the best I can do.
Any suggestions for improving the code? Thanks in advance for any help you're able to offer.
You can change all of the sampled values at once with b[spot] = 1 - b[spot]
I am looking for help with pseudo code (unless you are a user of Game Maker 8.0 by Mark Overmars and know the GML equivalent of what I need) for how to generate a list / array of unique combinations of a set of X number of integers which size is variable. It can be 1-5 or 1-1000.
For example:
IntegerList{1,2,3,4}
1,2
1,3
1,4
2,3
2,4
3,4
I feel like the math behind this is simple I just cant seem to wrap my head around it after checking multiple sources on how to do it in languages such as C++ and Java. Thanks everyone.
As there are not many details in the question, I assume:
Your input is a natural number n and the resulting array contains all natural numbers from 1 to n.
The expected output given by the combinations above, resembles a symmetric relation, i. e. in your case [1, 2] is considered the same as [2, 1].
Combinations [x, x] are excluded.
There are only combinations with 2 elements.
There is no List<> datatype or dynamic array, so the array length has to be known before creating the array.
The number of elements in your result is therefore the binomial coefficient m = n over 2 = n! / (2! * (n - 2)!) (which is 4! / (2! * (4 - 2)!) = 24 / 4 = 6 in your example) with ! being the factorial.
First, initializing the array with the first n natural numbers should be quite easy using the array element index. However, the index is a property of the array elements, so you don't need to initialize them in the first place.
You need 2 nested loops processing the array. The outer loop ranges i from 1 to n - 1, the inner loop ranges j from 2 to n. If your indexes start from 0 instead of 1, you have to take this into consideration for the loop limits. Now, you only need to fill your target array with the combinations [i, j]. To find the correct index in your target array, you should use a third counter variable, initialized with the first index and incremented at the end of the inner loop.
I agree, the math behind is not that hard and I think this explanation should suffice to develop the corresponding code yourself.
As a background, I'm a computer programmer and I'm working on a software library that allows a computer to quickly search through all dates to find a set of dates that satisfies a criteria. For example:
I want a list of every possible time that has ever occurred that has occurred on a friday or a saturday that is in April or May during the first week of the month.
My library uses numerical sets to efficiently represent ranges of dates that satisfy a criteria.
I've been thinking about ways to improve the performance of some parts of the app and I think that by combining sets and some geometry, I can really improve my results. However, my geometry is a bit rusty and I was hoping you might could help.
Here's my thought:
Certain elements of time can be represented as a circular dial. For example, Minutes can be positioned on a clock with values between 0...59. We could store valid ranges as a list of arcs. For example, If we wanted all times that ended with 05..10, we could store [5,10]. If we wanted all times that end with :45-59 or :00-15, we could store [45, 15]. Notice how this last arc "loops around" the dial. Here's a mockup showing different ranges intersecting on a dial
My question is this:
Given a set of whole numbers between N...M arranged into a circle.
Given Arc1 which is representing by [A, B] and Arc2 which is represented by [C, D] where A, B, C, and D are all within in range N...M
How do I determine:
A. Whether the arcs intersect.
B. If they do, what their intersection is.
C. If they do, what their union is.
Thank you so much for your help. If you're not able to help, if you can point me in the right direction, that would be great.
Thanks!
A simple and safe approach is to split the intervals that straddle 0. Then you perform pairwise interval intersection/union (for instance if A < D and C < B then [max(A,C), min(B,D)] for the intersection), and merge them if they meet at 0.
It seems the primitive operation to implement would be something like 'is the number X contained in the arch [A,B]'. Once you have that, you could implement an [A,B]/[C,D] arch-intersection predicate by something like -
Arch intersection means exactly that at least one of the following conditions is met:
C is contained in [A,B]
D is contained in [A,B]
A is contained in [C,D]
B is contained in [C,D]
One way to implement this contained-in-arch test without any branches is with some trigonometry and vector cross product. Not sure it would be faster (the math/branches performance tradeoff is entirely empiric), but it might be worth a try.
Denote Xa = sin(X/N * 2PI), Ya = cos(X/N * 2PI) and similarly for Xb,Yb etc.
C is contained in [A,B] is equivalent to:
Xa * Yc - Ya * Xc > 0
AND
Xc * Yb - Yc * Xb > 0
You can complete the other 3 conditions in an identical manner.
Hope this turns out useful.
I have 2 questions,
I've made a vector from a document by finding out how many times each word appeared in a document. Is this the right way of making the vector? Or do I have to do something else also?
Using the above method I've created vectors of 16 documents, which are of different sizes. Now i want to apply cosine similarity to find out how similar each document is. The problem I'm having is getting the dot product of two vectors because they are of different sizes. How would i do this?
Sounds reasonable, as long as it means you have a list/map/dict/hash of (word, count) pairs as your vector representation.
You should pretend that you have zero values for the words that do not occur in some vector, without storing these zeros anywhere. Then, you can use the following algorithm to compute the dot product of these vectors (pseudocode):
algorithm dot_product(a : WordVector, b : WordVector):
dot = 0
for word, x in a do
y = lookup(word, b)
dot += x * y
return dot
The lookup part can be anything, but for speed, I'd use hashtables as the vector representation (e.g. Python's dict).