Skip to content

Commit

Permalink
Merge branch 'master' into gb/initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaraldi authored Aug 26, 2024
2 parents 120fd25 + 6477530 commit b48ca01
Show file tree
Hide file tree
Showing 29 changed files with 280 additions and 66 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Standard library changes

#### Profile

* `Profile.take_heap_snapshot` takes a new keyword argument, `redact_data::Bool`,
that is `true` by default. When set, the contents of Julia objects are not emitted
in the heap snapshot. This currently only applies to strings. ([#55326])

#### Random

#### REPL
Expand Down
2 changes: 1 addition & 1 deletion base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ function __init__()
init_active_project()
append!(empty!(_sysimage_modules), keys(loaded_modules))
empty!(explicit_loaded_modules)
@assert isempty(loaded_precompiles)
empty!(loaded_precompiles) # If we load a packageimage when building the image this might not be empty
for (mod, key) in module_keys
loaded_precompiles[key => module_build_id(mod)] = mod
end
Expand Down
2 changes: 0 additions & 2 deletions base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ end
MustAlias(var::SlotNumber, @nospecialize(vartyp), fldidx::Int, @nospecialize(fldtyp)) =
MustAlias(slot_id(var), vartyp, fldidx, fldtyp)

_uniontypes(x::MustAlias, ts) = _uniontypes(widenconst(x), ts)

"""
alias::InterMustAlias
Expand Down
5 changes: 3 additions & 2 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArrayOrBroadcasted)
# use mapreduce_impl, which is probably better tuned to achieve higher performance
nslices = div(length(A), lsiz)
ibase = first(LinearIndices(A))-1
for i = 1:nslices
@inbounds R[i] = op(R[i], mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
for i in eachindex(R)
r = op(@inbounds(R[i]), mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
@inbounds R[i] = r
ibase += lsiz
end
return R
Expand Down
14 changes: 10 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,17 @@ hasgenerator(m::Core.MethodInstance) = hasgenerator(m.def::Method)

# low-level method lookup functions used by the compiler

unionlen(x::Union) = unionlen(x.a) + unionlen(x.b)
unionlen(@nospecialize(x)) = 1
unionlen(@nospecialize(x)) = x isa Union ? unionlen(x.a) + unionlen(x.b) : 1

_uniontypes(x::Union, ts) = (_uniontypes(x.a,ts); _uniontypes(x.b,ts); ts)
_uniontypes(@nospecialize(x), ts) = (push!(ts, x); ts)
function _uniontypes(@nospecialize(x), ts::Array{Any,1})
if x isa Union
_uniontypes(x.a, ts)
_uniontypes(x.b, ts)
else
push!(ts, x)
end
return ts
end
uniontypes(@nospecialize(x)) = _uniontypes(x, Any[])

function _methods(@nospecialize(f), @nospecialize(t), lim::Int, world::UInt)
Expand Down
4 changes: 4 additions & 0 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ end

"""
stat(file)
stat(joinpath...)
Return a structure whose fields contain information about the file.
The fields of the structure are:
Expand All @@ -218,16 +219,19 @@ The fields of the structure are:
| mtime | `Float64` | Unix timestamp of when the file was last modified |
| ctime | `Float64` | Unix timestamp of when the file's metadata was changed |
"""
stat(path) = (path2 = joinpath(path); path2 isa typeof(path) ? error("stat not implemented for $(typeof(path))") : stat(path2))
stat(path...) = stat(joinpath(path...))

"""
lstat(file)
lstat(joinpath...)
Like [`stat`](@ref), but for symbolic links gets the info for the link
itself rather than the file it refers to.
This function must be called on a file path rather than a file object or a file
descriptor.
"""
lstat(path) = (path2 = joinpath(path); path2 isa typeof(path) ? error("lstat not implemented for $(typeof(path))") : lstat(path2))
lstat(path...) = lstat(joinpath(path...))

# some convenience functions
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ where
.
->
::
[]
```

## Standard Modules
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Base.flipsign
Base.sqrt(::Number)
Base.isqrt
Base.Math.cbrt(::AbstractFloat)
Base.fourthroot(::Number)
Base.real
Base.imag
Base.reim
Expand Down
31 changes: 16 additions & 15 deletions doc/src/manual/mathematical-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,21 +551,22 @@ See [Conversion and Promotion](@ref conversion-and-promotion) for how to define

### Powers, logs and roots

| Function | Description |
|:------------------------ |:-------------------------------------------------------------------------- |
| [`sqrt(x)`](@ref), `√x` | square root of `x` |
| [`cbrt(x)`](@ref), `∛x` | cube root of `x` |
| [`hypot(x, y)`](@ref) | hypotenuse of right-angled triangle with other sides of length `x` and `y` |
| [`exp(x)`](@ref) | natural exponential function at `x` |
| [`expm1(x)`](@ref) | accurate `exp(x) - 1` for `x` near zero |
| [`ldexp(x, n)`](@ref) | `x * 2^n` computed efficiently for integer values of `n` |
| [`log(x)`](@ref) | natural logarithm of `x` |
| [`log(b, x)`](@ref) | base `b` logarithm of `x` |
| [`log2(x)`](@ref) | base 2 logarithm of `x` |
| [`log10(x)`](@ref) | base 10 logarithm of `x` |
| [`log1p(x)`](@ref) | accurate `log(1 + x)` for `x` near zero |
| [`exponent(x)`](@ref) | binary exponent of `x` |
| [`significand(x)`](@ref) | binary significand (a.k.a. mantissa) of a floating-point number `x` |
| Function | Description |
|:----------------------------- |:-------------------------------------------------------------------------- |
| [`sqrt(x)`](@ref), `√x` | square root of `x` |
| [`cbrt(x)`](@ref), `∛x` | cube root of `x` |
| [`fourthroot(x)`](@ref), `∜x` | fourth root of `x` |
| [`hypot(x, y)`](@ref) | hypotenuse of right-angled triangle with other sides of length `x` and `y` |
| [`exp(x)`](@ref) | natural exponential function at `x` |
| [`expm1(x)`](@ref) | accurate `exp(x) - 1` for `x` near zero |
| [`ldexp(x, n)`](@ref) | `x * 2^n` computed efficiently for integer values of `n` |
| [`log(x)`](@ref) | natural logarithm of `x` |
| [`log(b, x)`](@ref) | base `b` logarithm of `x` |
| [`log2(x)`](@ref) | base 2 logarithm of `x` |
| [`log10(x)`](@ref) | base 10 logarithm of `x` |
| [`log1p(x)`](@ref) | accurate `log(1 + x)` for `x` near zero |
| [`exponent(x)`](@ref) | binary exponent of `x` |
| [`significand(x)`](@ref) | binary significand (a.k.a. mantissa) of a floating-point number `x` |

For an overview of why functions like [`hypot`](@ref), [`expm1`](@ref), and [`log1p`](@ref)
are necessary and useful, see John D. Cook's excellent pair of blog posts on the subject: [expm1, log1p, erfc](https://www.johndcook.com/blog/2010/06/07/math-library-functions-that-seem-unnecessary/),
Expand Down
7 changes: 5 additions & 2 deletions src/gc-heap-snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ struct HeapSnapshot {
// global heap snapshot, mutated by garbage collector
// when snapshotting is on.
int gc_heap_snapshot_enabled = 0;
int gc_heap_snapshot_redact_data = 0;
HeapSnapshot *g_snapshot = nullptr;
// mutex for gc-heap-snapshot.
jl_mutex_t heapsnapshot_lock;
Expand All @@ -195,7 +196,7 @@ void _add_synthetic_root_entries(HeapSnapshot *snapshot) JL_NOTSAFEPOINT;


JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *nodes, ios_t *edges,
ios_t *strings, ios_t *json, char all_one)
ios_t *strings, ios_t *json, char all_one, char redact_data)
{
HeapSnapshot snapshot;
snapshot.nodes = nodes;
Expand All @@ -207,6 +208,7 @@ JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *nodes, ios_t *edges,

// Enable snapshotting
g_snapshot = &snapshot;
gc_heap_snapshot_redact_data = redact_data;
gc_heap_snapshot_enabled = true;

_add_synthetic_root_entries(&snapshot);
Expand All @@ -216,6 +218,7 @@ JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *nodes, ios_t *edges,

// Disable snapshotting
gc_heap_snapshot_enabled = false;
gc_heap_snapshot_redact_data = 0;
g_snapshot = nullptr;

jl_mutex_unlock(&heapsnapshot_lock);
Expand Down Expand Up @@ -328,7 +331,7 @@ size_t record_node_to_gc_snapshot(jl_value_t *a) JL_NOTSAFEPOINT

if (jl_is_string(a)) {
node_type = "String";
name = jl_string_data(a);
name = gc_heap_snapshot_redact_data ? "<redacted>" : jl_string_data(a);
self_size = jl_string_len(a);
}
else if (jl_is_symbol(a)) {
Expand Down
2 changes: 1 addition & 1 deletion src/gc-heap-snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static inline void gc_heap_snapshot_record_finlist(jl_value_t *finlist, size_t i
// Functions to call from Julia to take heap snapshot
// ---------------------------------------------------------------------
JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *nodes, ios_t *edges,
ios_t *strings, ios_t *json, char all_one);
ios_t *strings, ios_t *json, char all_one, char redact_data);


#ifdef __cplusplus
Expand Down
30 changes: 18 additions & 12 deletions stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ end
throw(ArgumentError(LazyString(lazy"cannot set entry ($i, $j) off the ",
A.uplo == 'U' ? "upper" : "lower", " bidiagonal band to a nonzero value ", x)))
end
return x
return A
end

Base._reverse(A::Bidiagonal, dims) = reverse!(Matrix(A); dims)
Expand Down Expand Up @@ -535,12 +535,18 @@ function rmul!(B::Bidiagonal, D::Diagonal)
end

@noinline function check_A_mul_B!_sizes((mC, nC)::NTuple{2,Integer}, (mA, nA)::NTuple{2,Integer}, (mB, nB)::NTuple{2,Integer})
# check for matching sizes in one column of B and C
check_A_mul_B!_sizes((mC,), (mA, nA), (mB,))
# ensure that the number of columns in B and C match
if nB != nC
throw(DimensionMismatch(lazy"second dimension of output C, $nC, and second dimension of B, $nB, must match"))
end
end
@noinline function check_A_mul_B!_sizes((mC,)::Tuple{Integer}, (mA, nA)::NTuple{2,Integer}, (mB,)::Tuple{Integer})
if mA != mC
throw(DimensionMismatch(lazy"first dimension of A, $mA, and first dimension of output C, $mC, must match"))
elseif nA != mB
throw(DimensionMismatch(lazy"second dimension of A, $nA, and first dimension of B, $mB, must match"))
elseif nB != nC
throw(DimensionMismatch(lazy"second dimension of output C, $nC, and second dimension of B, $nB, must match"))
end
end

Expand All @@ -563,8 +569,10 @@ _mul!(C::AbstractMatrix, A::BiTriSym, B::TriSym, _add::MulAddMul) =
_mul!(C::AbstractMatrix, A::BiTriSym, B::Bidiagonal, _add::MulAddMul) =
_bibimul!(C, A, B, _add)
function _bibimul!(C, A, B, _add)
require_one_based_indexing(C)
check_A_mul_B!_sizes(size(C), size(A), size(B))
n = size(A,1)
iszero(n) && return C
n <= 3 && return mul!(C, Array(A), Array(B), _add.alpha, _add.beta)
# We use `_rmul_or_fill!` instead of `_modify!` here since using
# `_modify!` in the following loop will not update the
Expand Down Expand Up @@ -727,15 +735,10 @@ end

function _mul!(C::AbstractVecOrMat, A::BiTriSym, B::AbstractVecOrMat, _add::MulAddMul)
require_one_based_indexing(C, B)
check_A_mul_B!_sizes(size(C), size(A), size(B))
nA = size(A,1)
nB = size(B,2)
if !(size(C,1) == size(B,1) == nA)
throw(DimensionMismatch(lazy"A has first dimension $nA, B has $(size(B,1)), C has $(size(C,1)) but all must match"))
end
if size(C,2) != nB
throw(DimensionMismatch(lazy"A has second dimension $nA, B has $(size(B,2)), C has $(size(C,2)) but all must match"))
end
iszero(nA) && return C
(iszero(nA) || iszero(nB)) && return C
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
nA <= 3 && return mul!(C, Array(A), Array(B), _add.alpha, _add.beta)
l = _diag(A, -1)
Expand All @@ -758,9 +761,10 @@ end
function _mul!(C::AbstractMatrix, A::AbstractMatrix, B::TriSym, _add::MulAddMul)
require_one_based_indexing(C, A)
check_A_mul_B!_sizes(size(C), size(A), size(B))
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
n = size(A,1)
m = size(B,2)
(iszero(m) || iszero(n)) && return C
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
if n <= 3 || m <= 1
return mul!(C, Array(A), Array(B), _add.alpha, _add.beta)
end
Expand Down Expand Up @@ -793,11 +797,12 @@ end
function _mul!(C::AbstractMatrix, A::AbstractMatrix, B::Bidiagonal, _add::MulAddMul)
require_one_based_indexing(C, A)
check_A_mul_B!_sizes(size(C), size(A), size(B))
m, n = size(A)
(iszero(m) || iszero(n)) && return C
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
if size(A, 1) <= 3 || size(B, 2) <= 1
return mul!(C, Array(A), Array(B), _add.alpha, _add.beta)
end
m, n = size(A)
@inbounds if B.uplo == 'U'
for i in 1:m
for j in n:-1:2
Expand All @@ -824,6 +829,7 @@ function _dibimul!(C, A, B, _add)
require_one_based_indexing(C)
check_A_mul_B!_sizes(size(C), size(A), size(B))
n = size(A,1)
iszero(n) && return C
n <= 3 && return mul!(C, Array(A), Array(B), _add.alpha, _add.beta)
_rmul_or_fill!(C, _add.beta) # see the same use above
iszero(_add.alpha) && return C
Expand Down
3 changes: 3 additions & 0 deletions stdlib/LinearAlgebra/src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ end
# allow packages like SparseArrays.jl to hook into here and redirect to out-of-place `cholesky`
_cholesky(A::AbstractMatrix, args...; kwargs...) = cholesky!(A, args...; kwargs...)

# allow cholesky of cholesky
cholesky(A::Cholesky) = A

## With pivoting
"""
cholesky(A, RowMaximum(); tol = 0.0, check = true) -> CholeskyPivoted
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function setindex!(D::Diagonal, v, i::Int, j::Int)
elseif !iszero(v)
throw(ArgumentError(lazy"cannot set off-diagonal entry ($i, $j) to a nonzero value ($v)"))
end
return v
return D
end


Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Base._reverse(A::Symmetric, ::Colon) = Symmetric(reverse(A.data), A.uplo == 'U'
@propagate_inbounds function setindex!(A::Symmetric, v, i::Integer, j::Integer)
i == j || throw(ArgumentError("Cannot set a non-diagonal index in a symmetric matrix"))
setindex!(A.data, v, i, j)
return A
end

Base._reverse(A::Hermitian, dims) = reverse!(Matrix(A); dims)
Expand All @@ -274,6 +275,7 @@ Base._reverse(A::Hermitian, ::Colon) = Hermitian(reverse(A.data), A.uplo == 'U'
else
setindex!(A.data, v, i, j)
end
return A
end

Base.dataids(A::HermOrSym) = Base.dataids(parent(A))
Expand Down
33 changes: 28 additions & 5 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,20 @@ _trimul!(C::AbstractMatrix, A::UpperOrLowerTriangular, B::AbstractTriangular) =
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::UpperOrLowerTriangular) =
generic_mattrimul!(C, uplo_char(B), isunit_char(B), wrapperop(parent(B)), A, _unwrap_at(parent(B)))

lmul!(A::AbstractTriangular, B::AbstractVecOrMat) = @inline _trimul!(B, A, B)
rmul!(A::AbstractMatrix, B::AbstractTriangular) = @inline _trimul!(A, A, B)

function lmul!(A::AbstractTriangular, B::AbstractVecOrMat)
if istriu(A)
_trimul!(B, UpperTriangular(A), B)
else
_trimul!(B, LowerTriangular(A), B)
end
end
function rmul!(A::AbstractMatrix, B::AbstractTriangular)
if istriu(B)
_trimul!(A, A, UpperTriangular(B))
else
_trimul!(A, A, LowerTriangular(B))
end
end

for TC in (:AbstractVector, :AbstractMatrix)
@eval @inline function _mul!(C::$TC, A::AbstractTriangular, B::AbstractVector, alpha::Number, beta::Number)
Expand Down Expand Up @@ -1017,8 +1028,20 @@ _ldiv!(C::AbstractVecOrMat, A::UpperOrLowerTriangular, B::AbstractVecOrMat) =
_rdiv!(C::AbstractMatrix, A::AbstractMatrix, B::UpperOrLowerTriangular) =
generic_mattridiv!(C, uplo_char(B), isunit_char(B), wrapperop(parent(B)), A, _unwrap_at(parent(B)))

ldiv!(A::AbstractTriangular, B::AbstractVecOrMat) = @inline _ldiv!(B, A, B)
rdiv!(A::AbstractMatrix, B::AbstractTriangular) = @inline _rdiv!(A, A, B)
function ldiv!(A::AbstractTriangular, B::AbstractVecOrMat)
if istriu(A)
_ldiv!(B, UpperTriangular(A), B)
else
_ldiv!(B, LowerTriangular(A), B)
end
end
function rdiv!(A::AbstractMatrix, B::AbstractTriangular)
if istriu(B)
_rdiv!(A, A, UpperTriangular(B))
else
_rdiv!(A, A, LowerTriangular(B))
end
end

# preserve triangular structure in in-place multiplication/division
for (cty, aty, bty) in ((:UpperTriangular, :UpperTriangular, :UpperTriangular),
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ Base._reverse!(A::SymTridiagonal, dims::Colon) = (reverse!(A.dv); reverse!(A.ev)
else
throw(ArgumentError(lazy"cannot set off-diagonal entry ($i, $j)"))
end
return x
return A
end

## Tridiagonal matrices ##
Expand Down Expand Up @@ -731,7 +731,7 @@ end
throw(ArgumentError(LazyString(lazy"cannot set entry ($i, $j) off ",
lazy"the tridiagonal band to a nonzero value ($x)")))
end
return x
return A
end

## structured matrix methods ##
Expand Down
Loading

0 comments on commit b48ca01

Please sign in to comment.