ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Jul 2017 21:17:23 -0500Several matrix multiplications over binary fieldshttp://ask.sagemath.org/question/38251/several-matrix-multiplications-over-binary-fields/Hi! I need to compute the product of several matrices with entries in a binary field. Since products do not depend on each other, computations can be easily distributed among the processors. However results of sequencial code and in processes are almost the same!
Here some code of a simple test that represents what I want to achieve:
from sage.all import *
import multiprocessing as mp
from multiprocessing.pool import ThreadPool, cpu_count
import time
def matrix_mult(A, B):
start = time.time()
A*B
return time.time() - start
def matrix_mult_proc(A, B, n):
avg = 0.0
for i in range(n):
start = time.time()
A*B
avg += (time.time() - start)
# print('time: {}'.format(time.time() - start))
print('Average per call proc {}: {}'.format(os.getpid(), avg / n))
@parallel
def matrix_mult_parallel(A, B):
start = time.time()
A*B
return time.time() - start
# print('time: {}'.format(time.time() - start))
iters = 40
n = 100
Fq = GF(2**16, 'X')
MS = MatrixSpace(Fq, n)
A = MS.random_element()
start = time.time()
tms = map(lambda i: matrix_mult(A, A), range(iters))
print('\n*** sequencial:\nAverage per call: {}\nTotal: {}'.format(sum(tms) / iters,time.time() - start))
nthreads = cpu_count()
pool = ThreadPool(nthreads)
start = time.time()
tms = pool.map(lambda i: matrix_mult(A, A), range(iters))
print('\n*** multithread {} threads:\nAverage per call: {}\nTotal: {}'.format(nthreads, sum(tms) / iters,time.time() - start))
nprocs = cpu_count()
procs = []
start = time.time()
print('\n*** multiproc {} procs:'.format(nprocs))
for i in range(nprocs):
p = mp.Process(target=matrix_mult_proc, args=(A, A, iters//nprocs))#, out_q))
procs.append(p)
p.start()
for p in procs:
p.join()
print('Total: {}'.format(time.time() - start))
args = [(A, A)]*iters
start = time.time()
tms = map(lambda e: e[1], list(matrix_mult_parallel(args)))
print('\n*** sage_parallel\nAverage per call: {}\nTotal {}'.format(sum(tms) / iters, time.time() - start))
Results are as follow:
For sequencial
Average time of a matrix multiplication: 0.279646992683
Total time for 40 multiplications: 11.1862668991
For Threadpool with 4 threads maximum
Average time of a matrix multiplication: 0.280531394482
Total time for 40 multiplications: 11.2248089314
For 4 processes in a 4 core computer (2 physical):
Average time of a matrix multiplication: 1.13726825714
Total time for 40 multiplications: 11.7641329765
With sage's @parallel decorator:
Average time of a matrix multiplication: 1.1256641984
Total time for 40 multiplications: 11.7641329765
I don't understand why multiplications seem to take a proportional amount of time to the number of processes. Same behavior on a 8 core machine.
Hope someone can explain. Thanks in advance.egonzalezThu, 13 Jul 2017 21:17:23 -0500http://ask.sagemath.org/question/38251/Find the kernel of a matrix $A$ and make it a matrix.http://ask.sagemath.org/question/33533/find-the-kernel-of-a-matrix-a-and-make-it-a-matrix/ I am trying to write a function that computes the monic genrator of an ideal $I\in k[x_1,\dots,x_n]$, i.e., the generator of $I\cap k[x_i]$ for each $i$. For this I need to use linear algebra for the set
$$\{1, x_i, x_i^2,\dots\}$$
I write each one of them in terms of the basis for the quotient ring $k[x_1,\dots,x_n]/I$, and see if they are linearly dependent.
Since I add in one more power a time, when I find a linearly dependent set, it should have nullity $1$. So if I can get the one element in basis of the kernel, I am done. But the $A.kernel()$ command in Sage gives me this:
N=M.kernel();N
Vector space of degree 2 and dimension 1 over Rational Field
Basis matrix:
[0 1]
Is there a way to assign it as a vector using the kernel command? Or do I have to write my own function to implement it? Thank you for your help!KittyLTue, 24 May 2016 06:47:57 -0500http://ask.sagemath.org/question/33533/Direct sum of matrix spaceshttp://ask.sagemath.org/question/11169/direct-sum-of-matrix-spaces/Is it possible to construct a direct sum of matrix spaces?
For example:
M1 = MatrixSpace(QQ, 1)
M2 = MatrixSpace(QQ, 2)
M = DirectSum(M1, M1, M2) # doesnt work
ThanksvuurSun, 23 Mar 2014 03:09:32 -0500http://ask.sagemath.org/question/11169/